C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-10-2004, 09:57 PM   #1
Registered User
 
Join Date: Sep 2004
Posts: 16
Trouble storing file input in array

I'm using the following to read a 3 line by 3 column file and storing it into the array. However, when I try to print out the array contents, only the first line is correct. The rest is either gibberish smiley faces or sometimes numbers.

Code:
   char read, array[3][3];
   int i=0, j=0;

   FILE *test = fopen("test.txt","r");
   while (!feof(test))
   {
      fscanf(test, "%c", &read);
      if (read=='\n')
         break;
      else
         array[i][j]=read;
      if (i<3 && j<3)
         j++;
      else if (i<3 && j==3)
         i++;
   }
difficult.name is offline   Reply With Quote
Old 10-10-2004, 11:54 PM   #2
Registered User
 
Join Date: Oct 2004
Posts: 11
Quote:
if (read=='\n')
break;
This code will break out of your read loop after the first line is read in. This will cause only 1 line to be read. The data inside the other parts of your array are junk characters because they weren't initialized.

for example you might have the file be something like:

abc
123
xyz

you code will read in abc and then detect a newline('\n') and break.

You may try:
Code:
while (!feof(test))
{
   fscanf(test, "%c", &read);
   if (read=='\n')
   {
      j = 0;  //reset j
      i++;   //increase to next line
      continue;  //go back to top of loop
   }
   j++;
}
hope this helps,
Ryan
PrimeTime00 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory Address kevinawad C++ Programming 18 10-19-2008 10:27 AM
Need Help Fixing My C Program. Deals with File I/O Matus C Programming 7 04-29-2008 07:51 PM
Trouble with file input w274v C Programming 6 12-18-2005 04:40 AM
Post... maxorator C++ Programming 12 10-11-2005 08:39 AM
Reading strings from a file and storing into an array Rizage C++ Programming 1 10-24-2002 03:04 AM


All times are GMT -6. The time now is 06:53 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22