473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Read text file into array

Hi ! I know it's possible to use a nested for loop or something to read an array and print it into a text file. Is it also possible to read in a text file and then put the values in an array ? Thanks !
Feb 7 '07 #1
10 31025
Ganon11
3,652 Expert 2GB
Definitely. What kind of text file will you be reading? What kind of array to you want to generate?
Feb 7 '07 #2
Definitely. What kind of text file will you be reading? What kind of array to you want to generate?
Cool ! I want to read in a file called file.txt which consists of N rows and N columns filled with values. I will need an array[N][N] that I can then use in code.
Feb 7 '07 #3
Ganon11
3,652 Expert 2GB
Do you know how many values there will be ahead of time? Is the number of rows different from the number of columns?
Feb 7 '07 #4
Do you know how many values there will be ahead of time? Is the number of rows different from the number of columns?
Yes, I know the number of rows and columns and they will be equal, so I know that the text file will have e.g. 20*20 values.
Feb 8 '07 #5
Ganon11
3,652 Expert 2GB
OK, then here's what you want to do:

1) Define a constant MAX_SIZE to the number of rows/columns (in your example, 20)
2) Create a(n) (insert type here) array of size N x N with the statement (int used for example):
Expand|Select|Wrap|Line Numbers
  1. int arr[MAX_SIZE][MAX_SIZE];
3) Create and open your ifstream variable to the input file
4)a. FOR LOOP from 0 to MAX_SIZE - 1 (index r)
Feb 8 '07 #6
Ganon11
3,652 Expert 2GB
4)b. FOR LOOP from 0 to MAX_SIZE - 1 (index c)
4)c. Input into arr[r][c];

Once the FOR loops finish, your array will be filled with the values from your input file.

Sorry for the 2 posts...the forum error has struck again. It wouldn't let me tack this little bit onto the end of the previous post.
Feb 8 '07 #7
Thanks, will point 3 and 4 be something like...

while(((c=fgetc(fp) !== EOF)
For (i=0; i <maxsize; i++){
For (j=0; j < maxsize; j++){
c=array[i][j];
}}

??
Feb 8 '07 #8
nmadct
83 Expert
Thanks, will point 3 and 4 be something like...

while(((c=fgetc(fp) !== EOF)
For (i=0; i <maxsize; i++){
For (j=0; j < maxsize; j++){
c=array[i][j];
}}

??
It should be

Expand|Select|Wrap|Line Numbers
  1. array[i][j] = c;
Also, you aren't accounting for the newline character at the end of each row.

You are using maxsize as the column/row count. This will only work if you're guaranteed to always have the same number of rows/columns. If that count can change, you'll need to count the number of columns in the first row before storing anything. This would be easier if you read the text line-by-line (using fgets) and then iterated over the characters in the line, rather than reading one character at a time.

If you do use fgets, keep in mind that it reads an entire line and leaves the newline character as the last character of the line. BUT, your last line might not have a newline, so don't blindly expect to have a newline unless your file is guaranteed to end with one.
Feb 8 '07 #9
Thnaks ! So can you alos write an entire line into an array ?

It should be

Expand|Select|Wrap|Line Numbers
  1. array[i][j] = c;
Also, you aren't accounting for the newline character at the end of each row.

You are using maxsize as the column/row count. This will only work if you're guaranteed to always have the same number of rows/columns. If that count can change, you'll need to count the number of columns in the first row before storing anything. This would be easier if you read the text line-by-line (using fgets) and then iterated over the characters in the line, rather than reading one character at a time.

If you do use fgets, keep in mind that it reads an entire line and leaves the newline character as the last character of the line. BUT, your last line might not have a newline, so don't blindly expect to have a newline unless your file is guaranteed to end with one.
Feb 8 '07 #10
nmadct
83 Expert
Thnaks ! So can you alos write an entire line into an array ?
You can make a loop to write a whole line to the array one character at a time, or you can use a function like strncpy or memcpy to copy the characters in bulk. No matter what you do, just be sure you don't go off the end of the array you're writing to.
Feb 14 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190,...
7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
9
by: srikanth | last post by:
i have a text file like below, test.txt file (actually my test file file is with 10000 lines but here i tested with 3 lines) 3 06.09.2006 16:37:25 3 06.09.2006 16:40:02 3 06.09.2006 16:42:31...
6
by: xdeath | last post by:
Hi guys, i've currently got an assignment, whereby, im supposed to create 2 classes, a Vehicle superclass, and a Taxi subclass. Vehicle class needs to have Reg Number, model, price, and Taxi is...
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
2
by: RyanS09 | last post by:
Hi- I have read many posts with specific applications of reading in text files into arrays, however I have not been able to successfully modify any for my application. I want to take a text file...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.