Я создаю таблицу с именем «последовательность», чтобы зарегистрировать номер в каждом маршрутизаторе, читаемый для текста «structure.txt». Номер маршрутизатора - «nb_input», номер пути, который мы можем выбрать, - «nb_output».
Например, если в тексте так: router1 1 3 4 , это означает последовательность [0] [0] = 1, последовательность [0] [2] = 1, последовательность [0] [3] = 1
Мой файл "structure.txt" выглядит так:
введите описание изображения здесь
У меня такой код. nb_input = 6, nb_output = 16 предопределено перед
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#define BUFFER_SIZE 1024
#define WHITESPACE 0x20
#define TAB 0x09
int main()
{
FILE *fStruct, *fPara;
char line[BUFFER_SIZE];
int i = 0, j=0;
char parameters_name[BUFFER_SIZE];
char parameters_value[BUFFER_SIZE];
int nb_input;
int nb_output;
int sum_size;
int b;
int c;
int l=0;
char s[10];
int k=0;
int start=0;
int sequence[nb_input][nb_output];
/*inisalisation of sequence array*/
for (i=0;i<nb_input;i++)
{
for (j=0;j<nb_output;j++)
{
sequence[i][j]=0;
}
}
fStruct = fopen("C:/Users/13383/Desktop/creater_noc/structure.txt","rt");
if(fStruct == NULL)
{
printf("ERROR: Can't open file.");
exit(1);
}
int r;
while(fgets(line, BUFFER_SIZE, fStruct) != NULL) // Read a line
{
i = 0, j=0,l=0,start=0,c=0,b=0,k=0;
memset(s, 0, sizeof(s));
while(line[i] == WHITESPACE || line[i] == TAB) //Skip Spaces or Tabs at the beginning of this line
{
i++;
}
if(line[i] == '\n') // Skip Blank lines
{
continue;
}
if(line[i] == '#') //The comments lines begin with a #
{
i++;
}
else
{
for(i=6;line[i]!='\n'&&line[i]!=-1;i++)
{
if(line[i]<='9'&&line[i]>='0')
{
if (start==1&&k==0)
{
b=atoi(s);
memset(s, 0, sizeof(s));
}
else if (start==2&&k==0)
{
c=atoi(s);
memset(s, 0, sizeof(s));
if (b>=1&&c>=1)
{
sequence[b-1][c-1]=1;
}
//printf("b=%d,c=%d\n",b,c);
}
s[k]=line[i];
for(;line[i+1]<='9'&&line[i+1]>='0';)
{
i++;
k++;
s[k]=line[i];
}
if (line[i-k-1]=='r')
{
start=1;
}
else if(line[i+1]=='\n')
{
c=atoi(s);
memset(s, 0, sizeof(s));
if (b>=1&&c>=1)
{
sequence[b-1][c-1]=1;
}
//printf("b=%d,c=%d\n",b,c);
continue;
}
else if(line[i+1]==EOF)
{
c=atoi(s);
memset(s, 0, sizeof(s));
if (b>=1&&c>=1)
{
sequence[b-1][c-1]=1;
}
//printf("b=%d,c=%d\n",b,c);
continue;
}
else
{
start=2;
}
//k++;
}
else if (start==1 || start==2 )
{
k=0;
}
}
}
}
}
Мой вопрос в том, что программа всегда не может хорошо прочитать последнюю строку, и я думаю, что это может быть из-за EOF. Кто-нибудь знает решение?