reading int and string from file fscanf

This is a discussion on reading int and string from file fscanf within the C Programming forums, part of the General Programming Boards category; Theres a text file that I have to read and it looks like this: 4 Taco Bell Tastes Fantastic 4-represents ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    reading int and string from file fscanf

    Theres a text file that I have to read and it looks like this:

    4
    Taco
    Bell
    Tastes
    Fantastic

    4-represents the "n" number of lines below it.

    I read the int using fscanf then I'm trying to read the rest of the lines and put them into an array of strings. Here's part of my code so far:


    Code:
    char *array[ MAX ];
    
    fscanf(file, "%d",  &numlines); //READS THE FIRST LINE WITH NUMBER
    	
    for( i = 0; i < numlines; i++) {
    	
    	//HOW DO I IMPLEMENT THIS SO I CAN STORE THE WORDS INTO AN ARRAY OF STRINGS "array"?
    
    		
    	}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can use fscanf in much the same way, unless you expect the words to have spaces in them (and you want to keep it that way), in which case you would presumably use fgets instead.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    if I put another fscanf within the for loop, I get a segfault

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then you didn't do fscanf very well, then. fscanf requires a format, and a place to put the read-in data. Presumably you need to give a valid place to put the data. Note that array[anything] is most emphatically not such a place.

    If you want to obtain a place to put data, you use malloc to do so. (Alternatively, you can make array an array of arrays, rather than an array of pointers -- arrays have their own memory that you can use.)

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Thumbs up

    Code:
    char *array[ MAX ];
    is an array of pointers, not an array of strings. It could be an array of pointers to strings, if they are allocated memory. MAX will be the number of pointers/strings, not the size of a string. So you will have to include that in your loop:
    Code:
    char *tmp;  /* to error-check malloc */
    char buffer[256];
    for( i = 0; i < numlines; i++) {
         fscanf("%s",buffer);
         if ((tmp=malloc(strlen(buffer)+1)) {    /* double brackets because we are "truth-testing" */
                 array[i]=tmp;
                 strcpy(array[i],buffer);
          }
    }
    This method is good if you are concerned about keeping memory use down, because each element of array will be exactly the right size. Also, char *array will be on the "heap" making it easier to pass and modify in other functions.
    However, you then have to remember to free() your malloced strings when done with them. If you don't need to do it this way, it is easier (on you and the processor) to just use an array of fixed size strings:
    Code:
    char  array[MAX][256];
    for( i = 0; i < numlines; i++) fscanf("%s",array[i]);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    sweet thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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