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

Click For Summary

Discussion Overview

The discussion revolves around reading a formatted text file into a dynamically allocated 2D array in C. Participants explore issues related to file reading, specifically how to skip the first integer in the file that indicates the size of the array, and how to correctly populate the array with subsequent values.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the task of reading a file containing an integer followed by a matrix of integers, and expresses difficulty in skipping the first integer when populating the 2D array.
  • Another participant suggests that if the dynamic array is allocated correctly, the issue may lie in how the values are being read from the file.
  • A participant shares a link to their code, indicating they are struggling with the loop used to read values into the array while attempting to skip the first number.
  • Questions are raised about how the initial size value is obtained and whether the reading loop is functioning correctly without repeating the same record.
  • One participant identifies a mistake regarding the use of the ampersand in the fscanf function, which they believe was causing their issue.
  • Another participant expresses initial confusion about the file handling, suggesting that reopening the file could lead to reading the first data element again, and inquires about the end-of-file test.
  • A participant confirms they resolved the infinite loop issue related to the feof function after receiving feedback.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method to skip the first number in the file, as the discussion includes various suggestions and corrections without a definitive solution being established.

Contextual Notes

Participants mention specific coding practices and functions in C, such as dynamic memory allocation, fscanf, and feof, but do not provide a complete resolution to the problem of reading the file correctly.

NDiggity
Messages
53
Reaction score
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
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.
 
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:
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?
 
I figured out my problem. I forgot the ampersand in front of the array in the fscanf function.
 
Last edited:
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)
 
Yes, I did fix the feof function. I ran it once and got an infinite loop before realizing that! Thanks for your help.
 

Similar threads

Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
8
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 10 ·
Replies
10
Views
12K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 18 ·
Replies
18
Views
3K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K