0

This is my code:

for(b = 0; b < 3; b++)
{
    int col1 = 0;
    printf("b= %d\t" , b);
    fgets(payload, sizeof payload, f2);
    fputs(payload, stdout);
    char *token;

    token = strtok(payload, " ");
    token = strtok(NULL, " ");  
    token = strtok(NULL, " ");

    while ( token != NULL)
    {
        int pp;
        sscanf(token, "%d", &pp);
        token = strtok(NULL, " ");
        printf("%d\n" ,pp);
        grapharray[b][col1++] = pp;
    }
}

In this code, I am taking some values from the file line by line and copying them into a 2D array. I am skipping the first two values from the file. Everything is working fine except my loop -- it copies the value correctly into location grapharray[b][col1], where b==0, but then skips b==1 and directly moves to b==2 and copies the next row of the file at grapharray[2][col1]. Can anyone help me with this problem? Thanks so much, I will be grateful.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
tariq
  • 1
  • 3
  • 2
    please fix the indentation in your code so it looks sane. – Sam Miller Apr 26 '11 at 18:48
  • oh.. sir please tell me how to do that – tariq Apr 26 '11 at 18:50
  • 2
    Capitalizing the beginning of your sentences wouldn't hurt either. – Emile Cormier Apr 26 '11 at 18:51
  • @tariq: I did it for you – BlackBear Apr 26 '11 at 18:51
  • @tariq select the code in the editor and then hit the curly brackets button –  Apr 26 '11 at 18:53
  • 1
    What is in the data file you are parsing? Could you give an example of the input? – don Apr 26 '11 at 18:54
  • @don : sir it is a csv file data is like numbers seperated by space i am reading a line seperating it by spaces and converting that token from string to int and putting it my array file is like 23 45 533 43 32 – tariq Apr 26 '11 at 18:57
  • This is one of those situations where you will probably need to post more that just a code fragment, and the input data too. Note that since the data appears to be space delimited, you could use a single fscanf() call to extract the token far more simply and with less code. `fscanf( "%*s %*s %d, %*s %*s", &pp )` will do most of the work for you. – Clifford Apr 26 '11 at 19:06
  • Also add the output of your program. By "skipping" b==1, do you mean that you are not seeing a line that says 'b=1' ? – Jay Elston Apr 27 '11 at 17:44

2 Answers2

3

if your second

token = strtok(NULL, " ");             

returns NULL, your while loop won't be entered and it will look as if b=2 was discarded => check with a debugger the value of token and maybe review your parser.

EDIT:

If your parsed data contain a tab (\t) instead of a space, this is likely to happen. Maybe you want to use " \t" in your tokenizer.

Bruce
  • 7,094
  • 1
  • 25
  • 42
  • sir but my all line have same number of entries – tariq Apr 26 '11 at 19:03
  • 1
    once again, check with a debugger or a printf: it's cheap and might narrow your field of investigation. If my supposition is true, then you can analyze this particular line or maybe dump your data... – Bruce Apr 26 '11 at 19:06
0

If you are trying to read lines, one at a time from a file, fgets() is not the best tool. If there are more characters in the line that you have room for in the target array, these characters will not be read.

See Using fgets to read strings from file in C

Community
  • 1
  • 1
Jay Elston
  • 1,978
  • 1
  • 19
  • 38