Thread: Extracting data out from txt file seperated by ";"

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    Extracting data out from txt file seperated by ";"

    Hi ,

    i am having this problem of extracting data out from txt file being seperated by ¨;¨.

    Example:

    How is your day?;Fine
    Where are you now?;At your house

    My code is as follow:

    Code:
    char *ques[20];
    char *ans[20];
    
    int i=0;
    FILE *fp;
    fp=fopen("qna.txt", "r");
    
    if ( fp == 0 )
    {
    printf( "Could not open file\n" );
    }
    else
    {
    while(fgets(fileInput, sizeof fileInput, fp))   
    {
    
    if(sscanf(fileInput, "%s,%s", &ques[i], &ans[i]) == 2)
    
    {
    
    i++;
    }
    
    }
    }
    fclose( fp );

    The difficulties i faced was that i am not able to successfully extract the data out from the text file and stored it into both que[] and ans[].

    Errors keep coming out from the "%s,%s". And i guess i might have key in the wrong placeholders..

    Anyone can help ? Thanks in advance...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    ques and ans are arrays of 20 pointers to char. You apparently have not actually allocated any space to store the strings that you want to read in. If you know that there is a maximum of 20 lines in the file, and that each string to be stored has a certain maximum length (that is not too large), then it may be simpler to just create an array of 20 arrays of char, thus there would be space to store the strings.

    Considering what you have now, and assuming that you did allocate space for the string, the problem that you are currently facing is that ques[i] is a pointer to char, so &ques[i] is a pointer to pointer to char, but %s expects a corresponding pointer to char. As such, changing &ques[i] to ques[i] would fix that problem. But there is more: %s reads until the first whitespace character. You could specify more precisely, e.g., %[^ ]s (I think), or perhaps use strtok() instead of sscanf().

    Incidentally, it looks like the separator is a semi-colon, not a comma.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    Thanks for the reply..I will try again and see if it would work....=)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with storing serial port data into txt file
    By inmaterichard in forum C Programming
    Replies: 2
    Last Post: 04-02-2008, 02:20 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM