如何在字符串中读取并传递换行符?
How do I read in a string pass the newline character?
我必须读入一个文件,例如
apple
grape
banana
并将其存储到一个字符串中,但是 fgets 只读取到换行符并停止,所以它只在 apple 中读取。
我该如何解决这个问题?或者如何将这三个词全部存储为单独的字符串?
char* readFile(const char *fileName)
{
FILE *inFile;
inFile=fopen(fileName, "r");
char **stringInFile;
stringInFile = malloc(sizeof(char*)*50);
char *data = fgets(stringInFile,50,inFile);
printf("%s", data);
fclose(inFile);
return data;
}
顺便说一下,这都是 C 语言。
fgets()
is only reading one Line by each call, and sets the file courser to the next line. If you want to read the fully file, you have to iterate it. To check if you are at the end, you can check for the EOF flag with feof()
。
结果,对我来说工作:
char* readFile(const char *fileName)
{
FILE *inFile;
inFile=fopen(fileName, "r");
char **stringInFile;
stringInFile = malloc(sizeof(char*)*50);
while(!feof(inFile))
{
printf("%s", fgets(stringInFile,50,inFile));
}
fclose(inFile);
return stringInFile;
}
而且,你不需要变量 data
- fgets() 第一个参数是一个字符数组,它自动存储在哪里(例如 Apple
在你的程序中)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** readFile(const char *fileName);
int main(int argc, char **argv){
char **data = readFile(argv[1]);
int i;
for(i=0; data[i] ; ++i){
puts(data[i]);
free(data[i]);
}
free(data);
return 0;
}
char** readFile(const char *fileName){
FILE *inFile;
inFile=fopen(fileName, "r");
char **stringInFile;
stringInFile = calloc(50, sizeof(char*));
char line[256];
int i = 0;
while(fgets(line, sizeof(line), inFile)){
char *p = strchr(line, '\n');
if(p)
*p = 0;
if(i < 50 - 1)
stringInFile[i++] = strdup(line);
}
fclose(inFile);
return stringInFile;
}
我必须读入一个文件,例如
apple
grape
banana
并将其存储到一个字符串中,但是 fgets 只读取到换行符并停止,所以它只在 apple 中读取。
我该如何解决这个问题?或者如何将这三个词全部存储为单独的字符串?
char* readFile(const char *fileName)
{
FILE *inFile;
inFile=fopen(fileName, "r");
char **stringInFile;
stringInFile = malloc(sizeof(char*)*50);
char *data = fgets(stringInFile,50,inFile);
printf("%s", data);
fclose(inFile);
return data;
}
顺便说一下,这都是 C 语言。
fgets()
is only reading one Line by each call, and sets the file courser to the next line. If you want to read the fully file, you have to iterate it. To check if you are at the end, you can check for the EOF flag with feof()
。
结果,对我来说工作:
char* readFile(const char *fileName)
{
FILE *inFile;
inFile=fopen(fileName, "r");
char **stringInFile;
stringInFile = malloc(sizeof(char*)*50);
while(!feof(inFile))
{
printf("%s", fgets(stringInFile,50,inFile));
}
fclose(inFile);
return stringInFile;
}
而且,你不需要变量 data
- fgets() 第一个参数是一个字符数组,它自动存储在哪里(例如 Apple
在你的程序中)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** readFile(const char *fileName);
int main(int argc, char **argv){
char **data = readFile(argv[1]);
int i;
for(i=0; data[i] ; ++i){
puts(data[i]);
free(data[i]);
}
free(data);
return 0;
}
char** readFile(const char *fileName){
FILE *inFile;
inFile=fopen(fileName, "r");
char **stringInFile;
stringInFile = calloc(50, sizeof(char*));
char line[256];
int i = 0;
while(fgets(line, sizeof(line), inFile)){
char *p = strchr(line, '\n');
if(p)
*p = 0;
if(i < 50 - 1)
stringInFile[i++] = strdup(line);
}
fclose(inFile);
return stringInFile;
}