-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.

6
  • 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(). Commented Oct 23, 2014 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! Commented Oct 23, 2014 at 18:00
  • Use fgets() to read the file. Commented Oct 23, 2014 at 18:00
  • fscanf(traceFile," %c",variable) is incorrect. You need to give the address of the storage, fscanf(traceFile," %c",&variable) Commented Oct 23, 2014 at 18:02
  • possible duplicate of reading in a variable length string user input in C Commented Oct 23, 2014 at 18:07

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

getline() is not in my C libraries (MS Visual C 2010)
@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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.