-3

I want to implement a function that reads data from a file. I can read data via fscanf, but the problem is that the length of the lines differ from line to line and I have to store them all. The file has the following format:

numberOfStates state1 state2 ... numberOfAlphabts alphabt1 ... transitiontable

What this means is that numberOfStates determines how many stateX will be after it. If its 5 then we have 0 1 2 3 4, if it was two then we would have 0 1, and its the same case with numberOfAlphabets. Lets say if it was 2 then we have a b after it; if it was 5 then we have a b c d e after it in that line. Also there is another problem: transitiontable is an array that stores all the combination of states and alphabets. For example if there were two states 0 1, and two alphabets a b then transtiontable would be:

a0 a1

b0 b1 

Can anyone guide me on how to implement this? I have already implemented it, if the data was read from STDIN and not a file. But I can't get my head around on how to initialize transitiontable inside fscanf.

aaa
  • 435
  • 8
  • 22
  • If you have it working from STDIN and were using `scanf()` then modify it to use `fscanf()`. If you were using `gets()` then move on to `fgets()`. – Weather Vane Oct 23 '14 at 17:56
  • @WeatherVane the problem is that fscanf(traceFile," %c",variable) has such a format. and if the variable is transitiontable, then how do i do it? transitiontable's size depends on the numberofstate and numberofalphabets which will be read from the same line! i can't initialize it! – aaa Oct 23 '14 at 18:00
  • Use `fgets()` to read the file. – ani627 Oct 23 '14 at 18:00
  • `fscanf(traceFile," %c",variable)` is incorrect. You need to give the address of the storage, `fscanf(traceFile," %c",&variable)` – Weather Vane Oct 23 '14 at 18:02
  • possible duplicate of [reading in a variable length string user input in C](http://stackoverflow.com/questions/7672560/reading-in-a-variable-length-string-user-input-in-c) – Sam Hanley Oct 23 '14 at 18:07
  • OP has not really defined his problem. Is it converting from STDIN input to file input? He already has it working from STDIN, so why are input string lengths now an issue? Hint, if the data itself defines the number of fields that follow, just read in each field at a time with `fscanf()`. Then you don't need to know the line length, or read the whole line. – Weather Vane Oct 23 '14 at 18:37

1 Answers1

4

Here is a simple program to read each line , regardless of its size.

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


int read_data(){

    char *line = NULL;
    size_t len = 0;
    char read;
    FILE *fp = fopen("data.txt" , "r") ; 
    if (!fp) {
        printf("No such file or directory.\n");
        return -1 ; 
    } 
    while ( read = getline(&line , &len, fp ) != -1 ) printf("%s", line);
    if (line) free(line) ; 
    return 0 ; 
}

From then on you can store line in an array if you want to keep it. Then for your problem you have to do some string parsing (which in C might be messy ) and get your data out of line variable.

billpcs
  • 633
  • 10
  • 17
  • `getline()` is not in my C libraries (MS Visual C 2010) – Weather Vane Oct 23 '14 at 18:09
  • @DoomProg suppose i use getline, how would i know the length of the line? how many characters do i read? the problem is that i don't know the length of the each line, it differs from line to line – aaa Oct 23 '14 at 19:55
  • If you took the time to follow @DoomProg's link and then googled "getline posix" you would have found that if null arguments are supplied to `getline()` (as in his example) it will allocate a buffer and return the buffer address and the line length. – Weather Vane Oct 23 '14 at 20:26