Read in a file and store in char array

Reply

Join Date: Jun 2005
Posts: 7
Reputation: dile is an unknown quantity at this point 
Solved Threads: 0
dile's Avatar
dile dile is offline Offline
Newbie Poster

Read in a file and store in char array

 
0
  #1
Jun 23rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Read in a file and store in char array

 
0
  #2
Jun 23rd, 2005
Post the code you have so far -- remember to use code tags: [code][/code].
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 7
Reputation: dile is an unknown quantity at this point 
Solved Threads: 0
dile's Avatar
dile dile is offline Offline
Newbie Poster

Re: Read in a file and store in char array

 
0
  #3
Jun 23rd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Read in a file and store in char array

 
0
  #4
Jun 23rd, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 7
Reputation: dile is an unknown quantity at this point 
Solved Threads: 0
dile's Avatar
dile dile is offline Offline
Newbie Poster

Re: Read in a file and store in char array

 
0
  #5
Jun 23rd, 2005
  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Read in a file and store in char array

 
0
  #6
Jun 23rd, 2005
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).

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]
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Read in a file and store in char array

 
0
  #7
Jun 23rd, 2005
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 )'.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 7
Reputation: dile is an unknown quantity at this point 
Solved Threads: 0
dile's Avatar
dile dile is offline Offline
Newbie Poster

Re: Read in a file and store in char array

 
0
  #8
Jun 24th, 2005
thank you so much for your help!
Now i can actually start on the assignment
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC