读取发生变化但该行的结构已知的文件行

发布于 2024-11-07 00:47:10 字数 851 浏览 5 评论 0原文

我有一个逐行组织的文件,所有行的结构都是由我定义的,但每行的内容可以根据同一行包含的两个变量增加或减少。

行示例: 1 Andre 2 0 5 13 05 2011 4 13 05 2011

'1' 代表用户 ID

相关联的名称

'Andre' 代表与 ID '2 0' 细分: '2' 代表如何细分用户带回家的电影数量和“0”表示用户预订了多少部电影。

到目前为止,该行遵循模式 %d %s %d %d,它们可以存储在我想要的变量中。

现在最困难的部分来了。根据“2 0”部分,程序知道必须读取模式 %d %d %d %d(“5 13 05 2011”和“4 13 05 2011” - 代表带回家的电影日期)2 次而且程序还知道,在读取前一个模式的两倍后,它知道由于“2 0”中的零部分,我不需要读取任何其他内容。

包含更多数据的行示例: 1 Andre 2 1 5 13 05 2011 4 13 05 2011 7 14 05 2011

'2 1'

“2”部分告诉程序在该行中应该读取“ 5 13 05 2011'和'4 13 05 2011'到variable_one[i][4](示例)

'1'部分告诉程序在该行中应该读取'7 14 05 2011'到variable_two[i][ 4](示例)

我正在使用 fscanf(file, "%d %s %d %d", &id[i],&nomes[i],&livros_num[i][0] ,&livros_num[i][1]); 读取 '1 Andre 2 0' 但我如何根据 '2 0' 读取该行的其余部分?

I have a file that is organized line by line and the structure of all lines was defined by me but the contents of 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'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猫卆 2024-11-14 00:47:10

您可以使用fscanf
这是一个例子:

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

You can use fscanf.
Here's an example:

int int1;
char char_buf[BUF_SIZE];
fscanf ( file_stream, "%d%s", &int1, char_buf);
一笑百媚生 2024-11-14 00:47:10

第一次调用 fscanf 后,您需要根据读取的变量进行循环。您似乎已经意识到,您在第一次调用中读取的 int 决定了您在该行中需要的额外读取次数。

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

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
夜唯美灬不弃 2024-11-14 00:47:10
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++;

}
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++;

}
万水千山粽是情ミ 2024-11-14 00:47:10

好吧,当您读取“2 0”时,您会得到两条信息:您需要循环查看带回家的电影日期的次数以及您需要循环读取所有预订的次数。

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", ...);
}

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", ...);
}
放飞的风筝 2024-11-14 00:47:10

怎么样

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]);
}

等等.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文