如何正确读取文件中的输入?
INITIALIZE 100
ALLOC 20
ALLOC 10
FREE 16
ALLOC 19
FILL 16 19 255
FILL 48 10 127
DUMP
SHOW ALLOCATIONS
ALLOC 1
SHOW USAGE
FINALIZE
这是说明的示例。如果我逐行插入它们,程序就可以运行。如果我从文件中读取所有内容,则会出现分段错误。从输入文件中读取它们很重要。问题可能出在哪里?澄清一下,我不需要读取程序中的文件,我需要将文件重定向到标准输入。这就是我将输入与命令进行比较的方式。
int main()
{
int n,c,d,e;
char *s, *p, init[100]="INITIALIZE", fin[100]="FINALIZE", dmp[100]="DUMP", allc[100]="ALLOC", fre[100]="FREE", fil[100]="FILL", shw[100]="SHOW", usg[100]="USAGE", alc[100]="ALLOCATIONS";
unsigned char *arena;
s=(char*)malloc(100*sizeof(char));
while (1)
{
gets(s);
p=strtok(s," ,.-");
while(p!=NULL)
{
if(strcmp(p,init)==0)
{
p=strtok(NULL," .,-");
n=atoi(p);
arena=initialize(n);
}
if(strcmp(p,fin)==0) { finalize(arena); }
if(strcmp(p,dmp)==0) { dump(arena,n); }
if(strcmp(p,allc)==0)
{
p=strtok(NULL," ,.-");
c=atoi(p);
alloc(c,arena,n);
}
if(strcmp(p,fre)==0)
{
p=strtok(NULL," ,.-");
c=atoi(p);
freeb(arena,c);
}
if(strcmp(p,fil)==0)
{
p=strtok(NULL," ,.-");
c=atoi(p);
p=strtok(NULL," ,.-");
d=atoi(p);
p=strtok(NULL," ,.-");
e=atoi(p);
fill(arena,c,d,e);
}
if(strcmp(p,shw)==0)
{
p=strtok(NULL," ,.-");
if(strcmp(p,fre)==0) { showfree(arena,n); }
if(strcmp(p,usg)==0) { showusage(arena,n); }
if(strcmp(p,alc)==0) { showallocations(arena,n); }
}
p=strtok(NULL," ,.-");
}
}
return 0;
}
INITIALIZE 100
ALLOC 20
ALLOC 10
FREE 16
ALLOC 19
FILL 16 19 255
FILL 48 10 127
DUMP
SHOW ALLOCATIONS
ALLOC 1
SHOW USAGE
FINALIZE
This is an example of instructions. If I insert them line by line, the programme works. If I read all of them from a file, I get segmentation fault. It is important to read them from input file. Where could the problem be? To clarify, I don't need to read the file in the programme, I need to redirect a file to the standard input. This is how I compare the input with my commands.
int main()
{
int n,c,d,e;
char *s, *p, init[100]="INITIALIZE", fin[100]="FINALIZE", dmp[100]="DUMP", allc[100]="ALLOC", fre[100]="FREE", fil[100]="FILL", shw[100]="SHOW", usg[100]="USAGE", alc[100]="ALLOCATIONS";
unsigned char *arena;
s=(char*)malloc(100*sizeof(char));
while (1)
{
gets(s);
p=strtok(s," ,.-");
while(p!=NULL)
{
if(strcmp(p,init)==0)
{
p=strtok(NULL," .,-");
n=atoi(p);
arena=initialize(n);
}
if(strcmp(p,fin)==0) { finalize(arena); }
if(strcmp(p,dmp)==0) { dump(arena,n); }
if(strcmp(p,allc)==0)
{
p=strtok(NULL," ,.-");
c=atoi(p);
alloc(c,arena,n);
}
if(strcmp(p,fre)==0)
{
p=strtok(NULL," ,.-");
c=atoi(p);
freeb(arena,c);
}
if(strcmp(p,fil)==0)
{
p=strtok(NULL," ,.-");
c=atoi(p);
p=strtok(NULL," ,.-");
d=atoi(p);
p=strtok(NULL," ,.-");
e=atoi(p);
fill(arena,c,d,e);
}
if(strcmp(p,shw)==0)
{
p=strtok(NULL," ,.-");
if(strcmp(p,fre)==0) { showfree(arena,n); }
if(strcmp(p,usg)==0) { showusage(arena,n); }
if(strcmp(p,alc)==0) { showallocations(arena,n); }
}
p=strtok(NULL," ,.-");
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在发布此类问题之前进行基本搜索。无论如何,如果您正在寻找使用 C 程序打开、读取、写入和关闭文件的代码片段/示例程序,请查看 此处
You could have done a basic search before posting one such question. Anyhow, if you're looking for code-snippets/example-programs to open, read, write and close files using a C program, please take a look at here