【发布时间】:2011-02-27 21:20:56
【问题描述】:
我正在从一个文件中读取,当我阅读时,它会逐行读取并打印出来
我真正想要的是我想要一个 char 数组来保存文件中的所有字符并打印一次,
这是我的代码
if(strcmp(str[0],"@")==0)
{
FILE *filecomand;
//char fname[40];
char line[100];
int lcount;
///* Read in the filename */
//printf("Enter the name of a ascii file: ");
//fgets(History.txt, sizeof(fname), stdin);
/* Open the file. If NULL is returned there was an error */
if((filecomand = fopen(str[1], "r")) == NULL)
{
printf("Error Opening File.\n");
//exit(1);
}
lcount=0;
int i=0;
while( fgets(line, sizeof(line), filecomand) != NULL ) {
/* Get each line from the infile */
//lcount++;
/* print the line number and data */
//printf("%s", line);
}
fclose(filecomand); /* Close the file */
【问题讨论】:
-
其实我想要的是整个文本文件内容保存在一个char数组中,不用于打印,我想稍后使用数组的char
-
Nadeem,请参阅他链接的已接受答案。这就是你想要的。基本上,bytes char* 就是你所说的数组,你可以用它做任何你想做的事情,直到你释放它。