Help Reading A .txt File Into A 2D Array In C

In summary, the user asks for a file path and reads in the first number ('n') which tells them how big the array they need to make should be. They allocate a dynamically allocated 2d array with the dimensions nxn. Then they need to make a copy of the file into the dynamically allocated array. They have a problem making a copy into their 2d array because they are using fscanf with for loops to read in each value, but the initial int('n') at the top of the file screws it up. They fix their problem by forgetting the ampersand in front of the array in the fscanf function.
  • #1
NDiggity
54
0

Homework Statement


The program is to read in a file full of numbers formatted like this: The first line contains a single int n. Following this line, there will be n lines with n ints each separated by whitespace.

So I have asked the user for a file path and read in the first number ('n') which tells me how big the array I need to make should be, and created a dynamically allocated 2d array with the dimensions nxn which is what we are supposed to do.

Then, I need to make a copy of this file into the dynamically allocated array. Then I need to check whether the file contains a magic array, meaning the sum of every row and column is identical, and report to the user where it is.

My problem is making a copy into my 2d array. I'm using fscanf with for loops to read in each value, but that initial int('n') at the top of the file screws it up. Is there any way I can get it to skip this number when it reads this in, because it screws up the contents of the array.

Here is an example of the file I need to copy:
3
1 2 3
3 2 1
2 1 3
 
Physics news on Phys.org
  • #2
If you managed to allocate the dynamic array correctly then you did the step you are looking for.
The problem seems to be you are undoing that step.
 
  • #3
I allocated the array but then I need to fill it with the values contained in the file which is the step I'm having trouble with. Here is the loop I made and then assigning the array the values contained in the file, except I want to skip that very first number in the file, which I don't know how to do.
http://rafb.net/p/ta7PNj72.html
 
Last edited by a moderator:
  • #4
How did you get the value for 'size' in the first place?
Why does the second loop not read in the same record over and over?
 
  • #5
I figured out my problem. I forgot the ampersand in front of the array in the fscanf function.
 
Last edited:
  • #6
Ok good.
Initially from your description I thought you were inadvertently closing infile and reopening it.
That would restart the stream back to the first data element "3".
Incidentally did you fix the feof test too?
My man page shows it should be (feof(infile) == 0)
 
  • #7
Yes, I did fix the feof function. I ran it once and got an infinite loop before realizing that! Thanks for your help.
 

What is a .txt file?

A .txt file is a plain text file that contains human-readable text. It can be opened and edited using any text editor, and is commonly used to store data in a simple and easily accessible format.

How do I read a .txt file in C?

To read a .txt file in C, you will need to use the fopen() function to open the file, and then use the fscanf() function to read the data from the file. You will also need to use a loop to read the data line by line and store it in a 2D array.

What is a 2D array?

A 2D array is a data structure that represents a table or grid of values. It is essentially a matrix with rows and columns, and allows for efficient storage and retrieval of data in a two-dimensional format.

How do I store data from a .txt file into a 2D array in C?

To store data from a .txt file into a 2D array in C, you will need to first declare the array with the appropriate dimensions. Then, you can use the fscanf() function to read each value from the file and store it in the corresponding location in the array.

What are some common errors when reading a .txt file into a 2D array in C?

Some common errors when reading a .txt file into a 2D array in C include not properly declaring the array with the correct dimensions, not using a loop to read and store the data, and not properly handling errors or unexpected data in the file. It is important to carefully test and debug your code to ensure that the data is being read and stored correctly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
1
Views
524
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
924
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top