70,037
社区成员
发帖
与我相关
我的任务
分享
/*read structure.txt*/
#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;
}
}
}
}
}
else if (line[i + 1] == EOF || line[i + 1] == '\0')//添加一个是否等于'\0'的判断
文件的末尾可能是'\0'
VS2015 C++环境调试
#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 = nullptr, *fPara = nullptr;
char line[BUFFER_SIZE];
int i = 0, j = 0;
char parameters_name[BUFFER_SIZE] = { 0 };
char parameters_value[BUFFER_SIZE] = { 0 };
const int nb_input = 6;
const int nb_output = 16;
int sum_size = 0;
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("d:\\structure.txt", "rt");
if (fStruct == NULL)
{
printf("ERROR: Can't open file.");
exit(1);
}
int r = 0;
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 || line[i + 1] == '\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);
continue;
}
else
{
start = 2;
}
//k++;
}
else if (start == 1 || start == 2)
{
k = 0;
}
}
}
}
return 0;
}