devish
devish

Reputation: 3

Read Txt file Language C

Hi guys I have this file struct:

0
2 4
0: 1(ab) 5(b)
1: 2(b) 6(a)
2: 0(a) 2(b)
3: 2(a) 6(b)
4: 5(ab)
5: 2(a) 6(b)
6: 4(b) 6(ab)

Each line will feed a struct with its data (numbers + letters).
What's the best way to read the line and get the strings I want?

Example:

0
2 4
0,1,ab,5,b
1,2,b,5,a
...

The lines may vary in size because we can have 1, 2, 3, .... numbers.

I already did it :

//struct 
#define MAX_ 20

struct otherstats{ //struct otherStats
  int conectstat[MAX_];//conection with others stats
  int transitions[MAX_];//Symbols betwen conection ASCI
}tableStats[MAX_];

struct sAutomate{
 int stat_initial; //initial
 int stats_finals[MAX_]; //final orfinals
 struct otherstats tableStats[MAX_]; //otherStats 0 1 2 3 4 5 6
};

/* eXample that what i want ..using the example 
sAutomate.stat_initial=0
sAutomate.stats_finals[0]=2
sAutomate.stats_finals[1]=4

Others Stats table
//0
sAutomate.tableStats[0].conectstat[0]=1;
sAutomate.tableStats[0].conectstat[1]=5;
sAutomate.tableStats[0].transitions[0]=ab;
sAutomate.tableStats[0].transitions[1]=b; 
//1
sAutomate.tableStats[1].conectstat[0]=2;
sAutomate.tableStats[1].conectstat[1]=6;
sAutomate.tableStats[1].transitions[0]=b;
sAutomate.tableStats[1].transitions[1]=a;
///etc
 */



void scanfile(){ //function to read the file

struct sAutomate st; //initialize st struct

char filename[] = "txe.txt";
FILE *file = fopen ( filename, "r" );
char buf[81];       
char parts[5][11];  

fscanf(file,"%d", &st.stat_initial);//read first line
printf("initial state : %d \n", st.stat_initial);
fscanf(file,"%d",&st.stats_finals);
fscanf(file,"%d",&st.stats_finals);


while (fgets(buf, sizeof(buf), stdin) != NULL)
{
if (sscanf(buf, "%10[^:]: (%10[^(], %10[^)]), (%10[^(], %10[^)])",
           parts[0], parts[1], parts[2], parts[3], parts[4]) == 5)
{
  printf("parts: %s, %s, %s, %s, %s\n",
         parts[0], parts[1], parts[2], parts[3], parts[4]);
}
else
{
  printf("Invalid input: %s", buf);
}
}
//fclose

Upvotes: 0

Views: 865

Answers (2)

effeffe
effeffe

Reputation: 2881

If you really don't know how many numbers and letters the line will contain, why are you reading a fixed amount of numbers and letters?

You could read the whole line with fgets and then parse it with a tokenizer like strtok, something like this:

const char* const DELIMITERS = " ";

int i;  // index for tableStats
char* token;

token = strtok(line, DELIMITERS);

// first integer
if (token == NULL || sscanf(token, "%d:", &i) < 1)
  // error

/* it seems like you should have at least one element in your "list",
 * otherwise this is not necessary
 */

token = strtok(NULL, DELIMITERS);

if (token == NULL || sscanf(token, "%d(%[^)])",
    &(tableStats[i].connectstat[0]),
    &(tableStats[i].transitions[0])) < 2)
  // error

// read optional part
for (int j = 1; (token = strtok(NULL, DELIMITERS)) != NULL; ++j)      
  if (sscanf(token, "%d(%[^)])", &(tableStats[i].connectstat[j]),
      &(tableStats[i].transitions[j])) < 3)
    break;

Remember that strtok changes the string, make a copy of it if you still need it.

Obviusly the code is for the arbitrary long lines, reading the first two lines is trivial.

Upvotes: 0

Mike
Mike

Reputation: 49473

First problem I see is you're overwriting stats_finals:

fscanf(file,"%d",&st.stats_finals);
fscanf(file,"%d",&st.stats_finals);

What you wanted to do here was:

fscanf(file,"%d",&st.stats_finals[0]);
fscanf(file,"%d",&st.stats_finals[1]);

To save off both the "2" and the "4" from the text file.

Second major problem is you're reading from stdin:

while (fgets(buf, sizeof(buf), stdin) != NULL)

That doesn't read your text file, that reads input from the keyboard... So you wanted that to be:

while (fgets(buf, sizeof(buf), file) != NULL)

Third (minor) problem is that fscanf() will not read newlines, and fgets() will. This means when you go from reading your second stats_finals to the first read in the while loop, your first input will just be the left over newline character. That's not a big deal since you check for "invalid input", but it's worth noting.

Finally, your sscanf looks wrong to me:

sscanf(buf, "%10[^:]: (%10[^(], %10[^)]), (%10[^(], %10[^)])",
               ^                        ^
              That's a width of 10,     Why are you checking for commas? You didn't
              I don't think that's      have any in your text file
              what you wanted...

I think this is more what you were looking for:

sscanf(buf, "%[0-9]: %[0-9](%[^)]) %[0-9](%[^)])",
                ^
             takes a digit (0 to 9)

EDIT Missed your original point. If you don't know how long the strings will be that you're reading, you can't use sscanf(). It's that simple. :)

The scanf family assumes you know how many objects you'll be parsing and the format string takes in that many. There are other options however.

Read a single line with fgets as you're doing, but then you can tokenize it. Either with the C function strtok or by your own hand with a for loop.

One note however:

Since you don't know how long it is, this: char parts[5][11]; is not your best bet. This limits you to 2 entries... probably it would be better to do this dynamically (read the line then allocate the correct size to store your tokens in.)

Upvotes: 1

Related Questions