Extracting data out from txt file

Microsoft Windows xp pro w/ sp3 multilin...
November 21, 2009 at 21:34:27
Specs: Linux i686
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:

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...
The language is c...


See More: Extracting data out from txt file

Report •


#1
November 22, 2009 at 01:28:27
homework, or for your work??

GNU win32 packages | Gawk


Report •

#2
November 22, 2009 at 05:35:38
home work...

Report •

#3
November 22, 2009 at 06:49:50
A few things:

char *ques[20];
char *ans[20];

You declare two arrays of char pointers, but I don't see you assigning any valid memory to them before their use. This is the source of your errors.

while(fgets(fileInput, sizeof fileInput, fp))
Where is this "fileInput" declared?

if(sscanf(fileInput, "%s,%s", &ques[i], &ans[i]) == 2)
sscanf() breaks on whitespace. A whitespace is defined as a space, a tab, or a newline; the semicolon is not one of these. string.h's strchr() might be what you're looking for.


Report •

Related Solutions


Ask Question