| | |
Read in a file and store in char array
![]() |
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
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
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
this is what i have so far:
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.
C Syntax (Toggle Plain Text)
#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.
In C, it may be better not to cast the return value of malloc. Checking the return value is always recommended. As is here. 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 {. Here is a place to fix up: I might go with something more like this. You are only reading into the first element of tempGrd. Later you pretend you have filled the whole thing up. 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.
C Syntax (Toggle Plain Text)
grid = (char *)malloc(sizeof(char) * 9 * 9);
C Syntax (Toggle Plain Text)
f_read = fopen(argv[1], "r");
This line was missing a {.
C Syntax (Toggle Plain Text)
for ( t = 0; t <50; t++ )
C Syntax (Toggle Plain Text)
while ( (num = fscanf(f_read, "%c", tempGrid))!= EOF )
C Syntax (Toggle Plain Text)
for ( i = 0; i < 50 && fscanf(f_read, "%c", &tempGrid[i]) == 1; i++ )
grid[i] = tempGrid[i];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
C Syntax (Toggle Plain Text)
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
•
•
•
•
Originally Posted by dile
C Syntax (Toggle Plain Text)
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
•
•
•
•
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
"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
You seem engaged in this, so I'll post my working copy.
C Syntax (Toggle Plain Text)
#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 */
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
![]() |
Similar Threads
- RE: reading .wav file and putting ito inot the array (C)
- Still need help with opening a file and store the data on it in an array (C++)
- While loop not ending when reading from file (C++)
- File parsing in 'C' (C)
Other Threads in the C Forum
- Previous Thread: Running external software/program
- Next Thread: File writing Error
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






