0

This code will read a file and print it as long as the file is in this format

word,12,34
words,40,20
another,20,11

How do I get it to do the same if its got an empty line in between like this because right now it just gives me a segmentation fault

word,12,34

words,40,20

another,20,11



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
    FILE *pdata;
    char buffer[1000];
    char *token, *del=",";
    int value1;
    double value2;


    pdata = fopen ("file.data", "r");

    if(pdata == NULL)
    {
        printf("unable to open file \n");
        exit(1);
    }

    while( fgets(buffer, sizeof(buffer), pdata) != NULL )
    {
        token = strtok(buffer, del);
        value1 = atoi( strtok(NULL, del) );
        value2 = atof( strtok(NULL, del) );

        printf("%s is %.3lf\n", token, value1 + value2);

    }

    fclose( pdata );


return 0;
}   

Please Help

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
Tatan1
  • 31
  • 6

2 Answers2

1

Two changes I would make:

char *del = ", "; // test for either space or comma

And (per my comment to the question earlier):

while( fgets(buffer, sizeof(buffer), pdata) != NULL )
{
    char *temp;
    token = strtok(buffer, del);
    if (token != NULL) {
      temp = strtok(NULL, del);
      if (temp) value1 = atoi( temp );
      temp = strtok(NULL, del);
      if (temp) {
        value2 = atof( temp );
        printf("%s is %.3lf\n", token, value1 + value2);
      }
}

The strtok will return NULL if no token is found. Passing a NULL string to atoi is what's giving you the seg fault, I'm guessing. Like this, we make absolutely sure that doesn't happen. I tested this and it works.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • still getting segmentation – Tatan1 Apr 21 '13 at 05:39
  • See my latest edits. I assign the result of each `strtok` to a `temp` variable and test it before performing `atoi` of `atof`. – Floris Apr 21 '13 at 05:44
  • i tried this still getting segmentation fault and my values are not adding correctly this is weird – Tatan1 Apr 21 '13 at 05:48
  • lol tried that last code and it skips a line but it prints crazy for example it printed word is 34 is 34 words is 20 is 20 lol – Tatan1 Apr 21 '13 at 05:54
  • typo when I transcribed... Using two computers - the one with C compiler is not currently online so I tested on one and re-typed code on the other. You need `atoi(temp)` not `atoi(strtok)`. Argggh. – Floris Apr 21 '13 at 05:56
  • you got it but why does it print the is at the end for example its printing word is 46 is 46 words is 60 is 60 – Tatan1 Apr 21 '13 at 06:01
  • Beats me - using exactly the code shown above? You get `word is 46 is 46` instead of `word is 46`? Try printing out the individual values? It makes no sense... Are you sure you have `double value2` and didn't change it to `int` when no-one was looking? Because strange things may happen when you pass the wrong size of parameter to `printf`. And you are not seeing (as you should with your format specifier `%.3lf`) `word is 46.000` – Floris Apr 21 '13 at 06:10
  • yeah double value2 is still the same and i even changed the print out statement to "token is %s", token; and it prints out token is word token is and so on – Tatan1 Apr 21 '13 at 06:19
  • you had it correct i had moved the print statement out of the last if and caused the issue – Tatan1 Apr 21 '13 at 06:34
  • i wish i had you as my tutor theres more to this code that i have to add to finish it and i have no idea how – Tatan1 Apr 21 '13 at 06:36
  • If you have another question, post it on the forum and add a link in a comment here so I can see it. No promises that I will have time to look at it... – Floris Apr 21 '13 at 17:46
0
while( fgets(buffer, sizeof(buffer), pdata) != NULL )
{
    if (buffer[0] == '\n') {
        /* empty line */
        continue;
    }
    token = strtok(buffer, del);
    value1 = atoi( strtok(NULL, del) );
    value2 = atof( strtok(NULL, del) );

    printf("%s is %.3lf\n", token, value1 + value2);

}
Barmar
  • 741,623
  • 53
  • 500
  • 612