944,196 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 28160
  • C RSS
Jun 23rd, 2005
0

Read in a file and store in char array

Expand Post »
I'm C newbie and i'm having a tough time with my first assignment

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dile is offline Offline
7 posts
since Jun 2005
Jun 23rd, 2005
0

Re: Read in a file and store in char array

Post the code you have so far -- remember to use code tags: [code][/code].
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 23rd, 2005
0

Re: Read in a file and store in char array

this is what i have so far:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. FILE *f_read;
  9. int num;
  10. char tempGrid[50];
  11. int i,t;
  12.  
  13. char *grid;
  14.  
  15. grid = (char *)malloc(sizeof(char) * 9 * 9);
  16.  
  17. if (argv[1] != NULL){
  18. f_read = fopen(argv[1], "r");
  19. while ((num = fscanf(f_read, "%c",tempGrid))!= EOF){
  20. for (i =0; i < 50; i++){ grid[i] = tempGrid[i];
  21. }
  22. }
  23. }
  24.  
  25. for (t = 0; t <50; t++)
  26. printf("%c\n", grid[t]);
  27. }
  28. return(0);
  29. }

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dile is offline Offline
7 posts
since Jun 2005
Jun 23rd, 2005
0

Re: Read in a file and store in char array

In C, it may be better not to cast the return value of malloc.
  1. grid = (char *)malloc(sizeof(char) * 9 * 9);
Checking the return value is always recommended. As is here.
  1. 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 {.
  1. for ( t = 0; t <50; t++ )
Here is a place to fix up:
  1. while ( (num = fscanf(f_read, "%c", tempGrid))!= EOF )
I might go with something more like this.
  1. 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 by sizeof(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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 23rd, 2005
0

Re: Read in a file and store in char array

  1. 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dile is offline Offline
7 posts
since Jun 2005
Jun 23rd, 2005
0

Re: Read in a file and store in char array

Quote originally posted by dile ...
  1. 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 of fscanf is compared to 1).

Quote originally posted by dile ...
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]
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 23rd, 2005
0

Re: Read in a file and store in char array

You seem engaged in this, so I'll post my working copy.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. char *grid = malloc(9 * 9);
  8. if ( grid )
  9. {
  10. int t, i = 0;
  11. if ( argc > 1 )
  12. {
  13. FILE *f_read = fopen(argv[1], "r");
  14. if ( f_read )
  15. {
  16. char tempGrid[50];
  17. puts("Just before while");
  18. while ( i < 50 && fscanf(f_read, "%c", &tempGrid[i]) == 1 )
  19. {
  20. if ( tempGrid[i] != ',' && !isspace(tempGrid[i]) )
  21. {
  22. grid[i] = tempGrid[i];
  23. ++i;
  24. }
  25. }
  26. }
  27. }
  28. for ( t = 0; t < i; t++ )
  29. {
  30. printf("%c\n", grid[t]);
  31. }
  32. free(grid);
  33. }
  34. return 0;
  35. }
  36.  
  37. /* my output
  38. H:\test>test file.txt
  39. Just before while
  40. 9
  41. 8
  42. 5
  43. 4
  44. 7
  45. 3
  46. 1
  47. 5
  48. */
Last edited by Dave Sinkula; Jun 23rd, 2005 at 11:58 pm. Reason: Changed 'if ( argc > 1 && argv[1] != NULL )' to 'if ( argc > 1 )'.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 24th, 2005
0

Re: Read in a file and store in char array

thank you so much for your help!
Now i can actually start on the assignment
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dile is offline Offline
7 posts
since Jun 2005
Oct 9th, 2010
0
Re: Read in a file and store in char array
Sorry, i am newbie...i want to ask to dave, where the read file command in the code?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daffi_gusti is offline Offline
8 posts
since Apr 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: help me in c++
Next Thread in C Forum Timeline: How to use strcmp correctly?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC