-1

I have a file with a lot of lines like this:

1:458:187:131
1:1124:384:63
1:2300:514:31
2:95:111:12
...

In each while of reading I use a function to use those values so that in next iteration it can be stored in the same variable.

and then I have other with strings

6:City1:46:795:825:17134:8398:616:323:8791:5873
7:City near London:507:1032
8:NY:2595:64:193:684:1258:341:1125:1956:5079

It's the same process as before. Any help?

void readst()
 {

      int nn1, nn2,nn3,nn4;
        char str1[200], str2[200];
   char str[200];
   FILE *fp;

   fp = fopen("file.txt", "r");
    if(!fp) return 1; // bail out if file not found
   while(fgets(str,sizeof(str),fp) != NULL)
   {
      // strip trailing '\n' if it exists
      int len = strlen(str)-1;
      if(str[len] == '\n') 
         str[len] = 0;
         int n1, n2,n3,n4;
        char str1[200], str2[200];

        sscanf(str,"%d:%d:%d:%d",&nn1,&nn2,&nn3,&nn4);
        printf("%d\n%d\n%d\n%d\n",nn1,nn2,nn3,nn4);

     printf("\n%s", str);
   }
   fclose(fp);
 }
MrFabio
  • 586
  • 2
  • 15
  • 31
  • 3
    What did you try? Did you hear of `getline`, `fscanf`, `sscanf` ? – Basile Starynkevitch Apr 14 '12 at 16:21
  • yes, i added what I made so for – MrFabio Apr 14 '12 at 16:35
  • 1
    http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c – skywall Apr 14 '12 at 16:36
  • 1
    You should really remove the unused variables before posting. `n1`..`n4` are unused; and neither of `str1` nor `str2` is used, even though they are declared twice (in two nested scopes). If you aren't getting compiler warnings about these, then you need to turn up the warning levels or get a better compiler. It is also generally a bad idea to 'shadow' variables, as the inner `str1` shadows or hides the outer one. – Jonathan Leffler Apr 14 '12 at 16:55
  • 1
    Your second file, with both spaces in names and variable numbers of fields in a line, is going to present some problems. How do you know which fields are expected and which are not? The `%[^:]` conversion specification (preferably with a length specified) might help you deal with the city names. – Jonathan Leffler Apr 14 '12 at 16:58

1 Answers1

1

Something like this ought to do the trick:

#include <stdio.h>

int main (void) {
    static const char filename[] = "file.txt";
    FILE *file = fopen ( filename, "r" );
    if (file != NULL) {
        char line [128];
        while (fgets(line, sizeof line, file) != NULL) {
            fputs(line, stdout);
        }
        fclose(file);
    }
    else {
        perror(filename);
    }
    return 0;
}

You can manipulate the variable inside the while loop.

To split a string:

p = strtok("a:b:c", ":");
while (p != NULL) {
    printf("%s\n", p);
    p = strtok(NULL, ":");
}
Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
  • `malloc.c:3096: sYSMALLOc: Assertion (old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. Aborted` it gives me on every test I made before, including this – MrFabio Apr 14 '12 at 16:33
  • 1
    I added the part for tokenizing to my post. – Charles Menguy Apr 14 '12 at 16:45
  • right, but it still generates that error even on a single sscanf like this `char f[] = "12345:4234:asd:asd"; int n1, n2; char str1[200], str2[200]; sscanf(f,"%d:%d:%[^:]s:%s",&n1,&n2,str1,str2);` – MrFabio Apr 14 '12 at 16:49
  • 1
    Why are you using scanf? Inside the while loop, the "line" variable will contain a line of your file, and you can then use the part for tokenizing that I added to my post which will split the line into tokens using ":" as a delimiter. – Charles Menguy Apr 14 '12 at 16:52
  • yeah it works in a single file, but using in my code it doesn't work. thanks I'll try to figure it out. thanks – MrFabio Apr 14 '12 at 16:58