C语言读取文件

雕兰箜篌 2020-07-14 11:36:40
我创建了一个数组"sequence" ,他可以存储读自"structure.txt"的每个router后的编号.router数量为 "nb_input", 我们可以选的数字范围是"nb_output".
比如,如果txt文件像这样:
router1 1 3 4
sequence里的对应位置会被赋值为1=>sequence[0][0]=1,sequence[0][2]=1,sequence[0][3]=1
我的"structure.txt" 是这样的:

我的代码是这样的. nb_input=6,nb_output=16 已被预先定义了

/*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;
}
}
}
}
}

我发现我的最后一行总是读不对,可能由于EOF的原因,请问大伙我的代码问题在哪?
...全文
105 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Simple-Soft 2020-07-15
  • 打赏
  • 举报
回复
参考一下 https://blog.csdn.net/zhanghaiyang9999/article/details/107032563
棉猴 2020-07-15
  • 打赏
  • 举报
回复
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;
}
雕兰箜篌 2020-07-15
  • 打赏
  • 举报
回复
引用 1 楼 棉猴 的回复:
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;
}
谢谢兄弟,现在代码可以运行了。我想允许每行末尾数字后面有空格也可以检测出来,请问怎么改进代码吖
雕兰箜篌 2020-07-15
  • 打赏
  • 举报
回复
感谢ddddd

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧