Thread: Help with reading a file in C, with fgets()/sscanf()

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Help with reading a file in C, with fgets()/sscanf()

    I have a text file with 190 lines, that goes something like this:

    Code:
    Antigua,English,Local dialects
    Bahamas,English,Creole
    Barbados,English
    Belize,English,Spanish,Mayan,Carib
    Canada,English,French
    Costa Rica,Spanish,English
    Cuba,Spanish
    First field is the country, following fields are the different languages spoken in that country.
    Since some countries have more languages than others, I am stuck trying to read the file using fgets() and sscanf().
    I'm doing this:
    Code:
    while(i < SIZE && fgets(buffer, 255, fp))
      {
        sscanf(buffer, "%[^,]%*c%[^\n]", holdName, buffer);//read the country first, and keep the rest for the languages 
        .........
        
        /* Here is where I'm lost, I want to read a language, keep the rest of the buffer to repeat until
            the buffer is empty. But obviously I'm doing it wrong */
        while(sscanf(buffer, "%[^,]%*c%[^\n]", holdLang, buffer) != '\n')
        {
          ............
        }
        i++;
      }
    Any help please?
    I have this big program to do, and can't do anything else until I am able to read the whole file.

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look into the strtok function. It will basically "break" the string into tokens at whatever characters you specify.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Another way is to make use of the %n conversion in scanf
    Code:
    #include <stdio.h>
    #include <string.h>
    int main ( ) {
      char test[] = "Belize,English,Spanish,Mayan,Carib\n";
      int   pos;
      char  *p = test;
      while ( *p ) {
        char word[50];
        sscanf(p,"%[^,\n]%*c%n",word,&pos);
        printf("Found Word=%s, pos=%d\n",word,pos);
        p += pos;
      }
      return 0;
    }
    
    $ gcc -W -Wall -ansi -pedantic -O2  bar.c
    $ ./a.out 
    Found Word=Belize, pos=7
    Found Word=English, pos=8
    Found Word=Spanish, pos=8
    Found Word=Mayan, pos=6
    Found Word=Carib, pos=6
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Thank you very much to both!
    I didnt know about any of those two methods.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file using fgets
    By Ferris in forum C Programming
    Replies: 10
    Last Post: 12-06-2010, 03:31 PM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  3. fgets, sscanf question.....
    By Ash1981 in forum C Programming
    Replies: 12
    Last Post: 01-06-2006, 01:31 PM
  4. Fgets + sscanf
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-15-2004, 08:53 PM
  5. fgets && sscanf
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 05:39 PM