1

I have a file that has three columns(readings from sensor) of varying length eg.

-728 -233 3947
4258 -623 333 
-500 93 -4141 
-491 107 -4136 
-495 94 -4161 
-886 101 -4148 
-2555 1864 -4888 

I use fgets to read the line. But I want to read the first column as x, second as y and third as z. Even if I use blank space as the mark for detecting a new substring, that wont solve my problem, since every line has different length of column digits. Any idea on how to do it? I am pasting a snippet of the code which reads the line from file and tries to print the three substrings as three array elements.

char *arr_x, *arr_y, *arr_z;

fp = fopen(filename, mode);
    while(! feof(fp))
    {
        if( fgets (str, 60, fp)!=NULL ) 
            {
                 strncpy(arr_x, str, ' ');
                 strncpy(arr_y, str, ' ');
                 puts(str);

            }
    }
HMM
  • 29
  • 1
  • 10

3 Answers3

2

I believe you're on right path, just need to add a bit more tweak. Please have a look at the following algo.

  1. Define three arrays, corresponding to x, y and z.
  2. Read the whole line.
  3. Start tokenizing the line using strtok() with space as delimiter, as you thought
  4. Store the first token in the i th index of x array, second token in y array, third token in z array.
  5. Continue to next line, increase i and keep on storing them until fgets() returns NULL.

Also, FWIW,

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • I referred to the link http://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm, but that shows "-" as delimiter, but I want space as delimiter. –  Nov 28 '15 at 08:17
  • @ShankdeepMukerji You can use any delimiter you like. BTW did you check the man page I linked in my answer? – Sourav Ghosh Nov 28 '15 at 08:18
  • Yes I did. Thanks for sharing the effect of using while ( !feof (file) ). So for using delim as blank space, should I use " "? –  Nov 28 '15 at 08:21
  • @ShankdeepMukerji Right. – Sourav Ghosh Nov 28 '15 at 08:22
  • if( fgets (str, 60, fp)!=NULL ) { token = strtok(str, s); while(token != NULL) { strcpy(ch_x, strtok(NULL, s)); printf("%s", ch_x); strcpy(ch_y, strtok(NULL, s)); printf("%s", ch_y); strcpy(ch_z, strtok(NULL, s)); printf("%s", ch_z); } p++; } } This throws error hello.c:171:10: error: incompatible types when assigning to type ‘char[7]’ from type ‘char *’ ch_z = strtok(NULL, s); –  Nov 28 '15 at 08:43
0

Try:

while(in != '\n') /* while (!fread(&in, 1, 1, p) */
{
     n++;
     arr = (char *) calloc (n, sizeof(char));
     scanf ("%c", &in);
}

Sorry couldn't indent on my cell.

AntonioCS
  • 8,335
  • 18
  • 63
  • 92
HMM
  • 29
  • 1
  • 10
0

Maybe using fscanf would be simpler?

I believe this is trusted data so there is no issue with buffer overflows, but if it isn't read the data with fgets (so that you limit the amount of data you read) and then use sscanf on the string.

Here is my code:

#include <stdio.h>

int main() {
    FILE *fp = fopen("read_file_space_delimited.txt", "r");

    if (fp) {        
        int x, y, z;

        while (fscanf(fp, "%d %d %d", &x, &y, &z) != EOF) {
            printf("Numbers: %d %d %d\n", x, y, z);
        }

        fclose(fp);
    }
}

The file read_file_space_delimited.txt has the data you provided in your question. After compiling and running this program this is the output I get:

Numbers: -728 -233 3947
Numbers: 4258 -623 333
Numbers: -500 93 -4141
Numbers: -491 107 -4136
Numbers: -495 94 -4161
Numbers: -886 101 -4148
Numbers: -2555 1864 -4888
AntonioCS
  • 8,335
  • 18
  • 63
  • 92