tags:

views:

114

answers:

4
+1  Q: 

C: sscanf problem

Hi
I have a text file like this:

2
A 10 5
B 31 2
C 6 6

I want to read first line number in a variable
and read each line's space separated list of 3 values in 3 variables. I wrote this code:

iF=fopen(fileName,"r");
fgets(tmp,255,iF);
sscanf(tmp,"%d",&interval);

while(!feof(iF)){
    cur=(P *)malloc(sizeof(P));
    fgets(tmp,255,iF);
    sscanf(tmp,"%c %d %d",&Name,&AT,&ET);
    cur->jobName=Name;
    cur->arrivalTime=AT;
    cur->execTime=ET;
    add_to_list(head,cur);
}

It works correctly for lines 1,3,4 but not for line 2! in line 2 it stores nothing! As I check in Debugger some strange chars are in the file (\342\200\252) and I don't know where are they from!?

What's the problem?

Thanks

+1  A: 

To debug this problem, do a few simple debugging steps:

After you fgets into tmp, try printing it out to see if it got what you expect.

You should store and display the return value of sscanf to see if it is succeeding.

abelenky
+2  A: 

Hmm...I'd expect to see a problem with the last line, but offhand don't see why you should have a problem elsewhere. OTOH, at least when I fix the obvious problem with how you're (attempting to) detect the end of the file, it seems to work fine for me:

#include <stdio.h>
#include <stdlib.h>

typedef struct P { 
    char jobName;
    int arrivalTime;
    int execTime;
} P;

void show_P(P const *r) { 
    printf("Name: %c, Arrival Time: %d, Execute Time: %d\n", r->jobName, r->arrivalTime, r->execTime);
}

int main() {
    int interval;
    char Name;
    int AT, ET;
    char tmp[256];

    FILE *iF=fopen("stupid_input.txt","r");

    fgets(tmp,255,iF);
    sscanf(tmp,"%d",&interval);

    while(fgets(tmp, 255, iF)){
        P *cur=malloc(sizeof(P));
        sscanf(tmp,"%c %d %d",&Name,&AT,&ET);
        cur->jobName=Name;
        cur->arrivalTime=AT;
        cur->execTime=ET;
        show_P(cur);
    }
    return 0;
}

Result:

Name: A, Arrival Time: 10, Execute Time: 5
Name: B, Arrival Time: 31, Execute Time: 2
Name: C, Arrival Time: 6, Execute Time: 6
Jerry Coffin
Hi, Thanks , your program has the same problem! I'm sure the problem is from the text file! at the beginning of second line there are these strange chars: \342\200\252 !
Snigger
Strange characters!! Just out of curiosity, have you edited the file on different OS(say windows) and trying to run on linux?
Praveen S
Hi, I made the file on linux and read it on the same linux.
Snigger
The strange characters you are getting is the UTF-8 encoding of `U+2028`, also called `LINE SEPARATOR` (see http://www.unicode.org/Public/6.0.0/charts/blocks/U2000.pdf). I don't know where you got it from, but it's most likely your text editor.
Roland Illig
+2  A: 

The following code works for me:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    const char *fileName = "data.txt";
    FILE *iF;
    int interval;
    char Name[256], tmp[256];
    int AT, ET;

    iF = fopen(fileName, "r");
    if (fgets(tmp, 255, iF) == NULL)
        goto error;
    if (sscanf(tmp, "%d", &interval) != 1)
        goto error;

    while (fgets(tmp, 255, iF) != NULL) {
        if (sscanf(tmp, "%c %d %d", Name, &AT, &ET) != 3)
            goto error;
        fprintf(stdout, "%s, %d, %d\n", Name, AT, ET);
    }
    return 0;

error:
    fprintf(stderr, "error while reading from the file.\n");
    return 1;
}

Note how much of the code is dedicated to error handling. That's necessary.

Roland Illig
+1  A: 

The characters (\342\200\252) you are seeing are octal for 226, 128, and 170. Those do not map to characters you would accidentally type in the input file. My recommendation would be to examine your input file with a hex editor, and see if there is any garbage in there.

If not, you can try changing how you read the file to see if the glitch is in your code, for example you might try fscanf (which is not usually preferred):

FILE *iF = fopen("input.txt", "r");
if (fscanf(iF, "%d\n", &interval) != 1)
{
  fprintf(stderr, "Error parsing interval.\n");
  fclose(iF);
  return;
}

while (! feof(iF))
{
   cur = (P *)malloc(sizeof(P));
   if (fscanf(iF, "%c %d %u\n",&cur->jobName, &cur->arrivalTime, &cur->execTime) != 3)
   {
     fprintf(stderr, "Error parsing line.\n");
     free(cur);
     break;
   }
   add_to_list(head,cur);
}

However, since others have posted several methods that worked for them and not for you, the input file is certainly a candidate for error.

Brandon Horsley