954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read in a file and store in char array

I'm C newbie and i'm having a tough time with my first assignment :sad:

The assignment is on creating a Number Puzzle.....apparently it's the newest craze in Toronto and my prof is a big fan....unfortunately

I'm trying to read in a file which looks like this:
9,8, ,5,,4,,
7,3,1, ,5

this is what i want to do..
-Read in the file and use fscanf to store everything in an array of chars called tempGrid. (I don't want the extra white space between commas)
-then tempGrid to another 1D array pointed to by a pointer called *grid.....WITHOUT the commas.
-And just print out the result

Obviously this is not my whole assignment, only like 1% of it.
I've tried to do this simple thing for two days now and i can't seem to get it to work.
PLEASE HELP!!

Another question: does fscanf write the result straight to an array or do i have to pass in a pointer that points to an array?

Thanks for any help. I really appreciate it. I'm on the verge of tearing my hair out in frustration :sad:

dile
Newbie Poster
7 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Post the code you have so far -- remember to use code tags : [code][/code].

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

this is what i have so far:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main(int argc, char *argv[])
{
	FILE *f_read;
	int num;
	char tempGrid[50];
	int i,t;

	char *grid;

	grid = (char *)malloc(sizeof(char) * 9 * 9);

	if (argv[1] != NULL){
		f_read = fopen(argv[1], "r");
            	while ((num = fscanf(f_read, "%c",tempGrid))!= EOF){
			for (i =0; i < 50; i++){							grid[i] = tempGrid[i];
			}
		}
	}

	for (t = 0; t <50; t++)
		printf("%c\n", grid[t]);
	}
	return(0);
}


when i run it the characters the array stores is some other weird characters. I think i need to do some kind of conversion after the while loop before i store the chars.

dile
Newbie Poster
7 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

In C, it may be better not to cast the return value of malloc.

grid = (char *)malloc(sizeof(char) * 9 * 9);


Checking the return value is always recommended. As is here.

f_read = fopen(argv[1], "r");


Checking return values will save you countless hours of debugging, so it's a good habit to start. Unless you like to pull your hair out for hours.

This line was missing a {.

for ( t = 0; t <50; t++ )


Here is a place to fix up:

while ( (num = fscanf(f_read, "%c", tempGrid))!= EOF )


I might go with something more like this.

for ( i = 0; i < 50 && fscanf(f_read, "%c", &tempGrid[i]) == 1; i++ )


You are only reading into the first element of tempGrd. Later you pretend you have filled the whole thing up.

grid[i] = tempGrid[i];


Multiplying bysizeof(char) is the long way of multiplying by one, which is pointless.

If the number of characters in the file may be less than your 50, it might be wise only to print out as many as were read.

Take another swing and we'll move forward from there.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
for ( i = 0; i < 50 && fscanf(f_read, "%c", &tempGrid[i]); i++ )


Could you explain that line a bit?
I don't really undestand how "i <50 && fscanf..." part works

And also the malloc line was given to us by the prof. He said that we have to use that exact line. He said it's his way of making sure we don't use a 2D array for the assignment :p

dile
Newbie Poster
7 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 
for ( i = 0; i < 50 && fscanf(f_read, "%c", &tempGrid[i]); i++ )

Could you explain that line a bit? I don't really undestand how "i <50 && fscanf..." part works

It just has two conditions to continue the loop -- that you don't overflow the buffer and that you successfully read some data (in the edited and fixed version the return value offscanf is compared to 1).

And also the malloc line was given to us by the prof. He said that we have to use that exact line. He said it's his way of making sure we don't use a 2D array for the assignment :p

[sarcasm=thin]Great -- another professor who needs a few more lessons.[/sarcasm]

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

You seem engaged in this, so I'll post my working copy.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char *argv[])
{
   char *grid = malloc(9 * 9);
   if ( grid )
   {
      int t, i = 0;
      if ( argc > 1 )
      {
         FILE *f_read = fopen(argv[1], "r");
         if ( f_read )
         {
            char tempGrid[50];
            puts("Just before while");
            while ( i < 50 && fscanf(f_read, "%c", &tempGrid[i]) == 1 )
            {
               if ( tempGrid[i] != ',' && !isspace(tempGrid[i]) )
               {
                  grid[i] = tempGrid[i];
                  ++i;
               }
            }
         }
      }
      for ( t = 0; t < i; t++ )
      {
         printf("%c\n", grid[t]);
      }
      free(grid);
   }
   return 0;
}

/* my output
H:\test>test file.txt
Just before while
9
8
5
4
7
3
1
5
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

thank you so much for your help! :D
Now i can actually start on the assignment

dile
Newbie Poster
7 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Sorry, i am newbie...i want to ask to dave, where the read file command in the code?

daffi_gusti
Newbie Poster
8 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You