开发者

Read a file line that changes but the structure of the line is known

开发者 https://www.devze.com 2023-03-06 05:37 出处:网络
I have a file that is organized line by line and the structure of all lines was defined by me but the contents o开发者_JS百科f each line can increase or decrease according to two variables that the sa

I have a file that is organized line by line and the structure of all lines was defined by me but the contents o开发者_JS百科f each line can increase or decrease according to two variables that the same line contains.

Line Example: 1 Andre 2 0 5 13 05 2011 4 13 05 2011

'1' represents the user ID

'Andre' represents the name associated to the ID

'2 0' subdivides: '2' represents how many movies the user as taken home AND '0' represents how many movies reservations the user has.

So far the line is following the pattern %d %s %d %d and they can be stored in the variables I want.

Now the hard part comes. Depending on the '2 0' part, the program knows that has to read the pattern %d %d %d %d ('5 13 05 2011' and '4 13 05 2011' - represent dates of movies taken home) 2 times and also the program knows that after reading 2 times the previous pattern it knows that i doesn't need to read anything else due to the zero part in '2 0'.

Example of line with more data: 1 Andre 2 1 5 13 05 2011 4 13 05 2011 7 14 05 2011

'2 1'

The '2' part tells the program that in that line it should read '5 13 05 2011' and '4 13 05 2011' to variable_one[i][4] (example)

The '1' part tells the program that in that line it should read '7 14 05 2011' to variable_two[i][4] (example)

I'm using fscanf(file, "%d %s %d %d", &id[i],&nomes[i],&livros_num[i][0],&livros_num[i][1]); to read '1 Andre 2 0' but how can i read the rest of the line according to '2 0'?


You can use fscanf. Here's an example:

int int1;
char char_buf[BUF_SIZE];
fscanf ( file_stream, "%d%s", &int1, char_buf);


After your first call of fscanf, you need to loop depending on the variables you read. You seem to have already realized that the int's you read in your first call determine the number of extra reads you need in the line.

int i;
for ( i=0; i< livros_num[i][0]; i++ ) {
    int something, day, month, year; // 'something' is unclear to me in your code
    fscanf( file, "%d %d %d %d", &something, &day, &month, &year );
    // store these variables wherever
}

// same procedure for second number
for ( i=0; i< livros_num[i][1]; i++ ) {
    int something, day, month, year; // 'something' is unclear to me in your code
    fscanf( file, "%d %d %d %d", &something, &day, &month, &year );
    // store these variables wherever
}
fscanf( file, "%s\n" ); // I'm NOT sure if this is nessecary, to move the file to the next line


fgets(buf,sizeof(buf),f)

p = strtok (buf," ");
p1 = strtok (0," ");
p2 = strtok (0," "); 

p3 = strtok (0," "); 

n1 = atoi(p1);
n2=  atoi(p2);

i=0;
while(n1--)
{
  sscanf(p3,"%d %d %d %d", &v1[i],&v2[i],&v3[i],&v4[i] );
  strok(0," ");
  strok(0," ");
  strok(0," ");
  p3 =   strok(0," ");
  i++;

}

i=0;
while(n2--)
{
  sscanf(p3,"%d %d %d %d", &v2[i],&v2[i],&v2[i],&v2[i] );
  strok(0," ");
  strok(0," ");
  strok(0," ");
  p3 =   strok(0," ");
  i++;

}


Well, when you read the '2 0' you get two pieces of information: the number times you need to loop for the dates of the movies taken home and the number of times you need to loop to read all the reservations.

int i;
int num_movies_taken, num_movies_reserved;

// read all the dates of the movies taken home
num_movies_taken = livros_num[i][0];
for (i = 0; i < num_movies_taken; i++)
{
    fscanf("%d %d %d %d", ...);
}

// read all the reservations
num_movies_reserved = livros_num[i][1];
for (i = 0; i < num_movies_reserved; i++)
{
    fscanf("%d %d %d %d", ...);
}


How about

int j;
fscanf(file, "%d %s %d %d", &id[i],&nomes[i],&livros_num[i][0],&livros_num[i][1]);
for (j=0;j<livros_num[i][0];++j)
{
    fscanf(file,"%d %d %d %d",&variable_one[0],&variable_one[1],&variable_one[2],&variable_one[3]);
}

etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消