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 !
Read text file into array
Collapse
X
-
Tags: None
-
Originally posted by Ganon11Definitely. What kind of text file will you be reading? What kind of array to you want to generate?Comment
-
Originally posted by Ganon11Do you know how many values there will be ahead of time? Is the number of rows different from the number of columns?Comment
-
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):
Code:int arr[MAX_SIZE][MAX_SIZE];
4)a. FOR LOOP from 0 to MAX_SIZE - 1 (index r)Comment
-
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.Comment
-
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];
}}
??Comment
-
Originally posted by dschulenburgThanks, 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];
}}
??
Code:array[i][j] = c;
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.Comment
-
Thnaks ! So can you alos write an entire line into an array ?
Originally posted by nmadctIt should be
Code:array[i][j] = c;
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.Comment
-
Originally posted by dschulenburgThnaks ! So can you alos write an entire line into an array ?Comment
Comment