用C逐行读取,但长度不正确
我的代码从文件中读取一行字符串,并在屏幕上打印它及其长度。但长度不正确(大于1)用C逐行读取,但长度不正确,c,C,我的代码从文件中读取一行字符串,并在屏幕上打印它及其长度。但长度不正确(大于1) #包括 #包括 #包括 int main(){ 文件*文件; file=fopen(“data.txt”、“r”); 字符*单线; 单线=(字符*)malloc(150*sizeof(字符*); 如果(文件){ 而(!feof(文件)){ fgets(单线,150,文件); puts(单线); printf(“长度:%ld\n”,strlen(单线)); } } fclose(文件); 返回0; } fgets(
#包括
#包括
#包括
int main(){
文件*文件;
file=fopen(“data.txt”、“r”);
字符*单线;
单线=(字符*)malloc(150*sizeof(字符*);
如果(文件){
而(!feof(文件)){
fgets(单线,150,文件);
puts(单线);
printf(“长度:%ld\n”,strlen(单线));
}
}
fclose(文件);
返回0;
}
fgets(char*s,int size,FILE*stream)
从流中最多读入size-1
字符,并将它们存储到s指向的缓冲区中。
EOF或换行符后,读取停止如果读取换行符,则将其存储到缓冲区中。“\0”存储在缓冲区中最后一个字符之后
如果行的长度小于149字节,并且不是文件的结尾,则换行符将被读取到buf。因此strlen的返回值将包括新行
另外:如果您只需要150个字符的缓冲区,而不是malloc(150*sizeof(char*))
,malloc(sizeof(char)*150)
可能更合理
不需要强制转换malloc的结果。有关详细信息,请回答此问题。您可以通过以下方式去除导致额外长度的换行符:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string>
int main()
{
char* singleLine;
FILE * file = fopen("data.txt","r");
singleLine = (char *) malloc(150* sizeof(char));
if(file)
{
char *p = NULL;
while(fgets(singleLine, 150, file) != NULL) //giving the length here directly as 150 is important, because using sizeof() or strlen() only reads the 1st 3 characters(which is the size of char * on my platform) of the string.
{
p = strstr(singleLine,"\n");
if(p)
{
*p = '\0';
}
printf("%s\n", singleLine);
printf("length: %ld\n",strlen(singleLine));
}
if (feof(file))
{
fclose(file);
}
else
{
printf("some other error occured");
}
}
else
{
printf("inside else\n");
}
return 0;
}
#包括
#包括
#包括
#包括
int main()
{
字符*单线;
FILE*FILE=fopen(“data.txt”、“r”);
单线=(char*)malloc(150*sizeof(char));
如果(文件)
{
char*p=NULL;
while(fgets(singleLine,150,file)!=NULL)//此处直接将长度指定为150很重要,因为使用sizeof()或strlen()只读取字符串的前3个字符(在我的平台上是char*的大小)。
{
p=strstr(单线,“\n”);
如果(p)
{
*p='\0';
}
printf(“%s\n”,单线);
printf(“长度:%ld\n”,strlen(单线));
}
if(feof(文件))
{
fclose(文件);
}
其他的
{
printf(“发生了其他错误”);
}
}
其他的
{
printf(“其他内部\n”);
}
返回0;
}
能否将文件内容也添加到您的问题中。如果可能的话,也发布输出。旁注:fclose(文件)
应在if
子句中。它是sizeof(char)
而不是sizeof(char*)
strlen()
返回一个size\t
,因此您应该使用%zu
格式说明符。singleLine=(char*)malloc(150*sizeof(char*))
您在这里分配了150个指针。谢谢,但我如何修复它?实现一个不计算换行符的strlen,或者在成功调用fgets
后从缓冲区中删除换行符。由于p
指向换行符,因此可以使用if(p!=NULL)*p='\0'代码>无需使用strlen()
。和。while(fgets(单线,150,文件)!=0)
(替换循环条件及其后的行)。您通常应该测试实际I/O操作的结果-fgets()
,getchar()
,scanf()
,等等。此外,使用单线[strcspn(singleLine,“\n”)]='\0'
以消除换行符。
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string>
int main()
{
char* singleLine;
FILE * file = fopen("data.txt","r");
singleLine = (char *) malloc(150* sizeof(char));
if(file)
{
char *p = NULL;
while(fgets(singleLine, 150, file) != NULL) //giving the length here directly as 150 is important, because using sizeof() or strlen() only reads the 1st 3 characters(which is the size of char * on my platform) of the string.
{
p = strstr(singleLine,"\n");
if(p)
{
*p = '\0';
}
printf("%s\n", singleLine);
printf("length: %ld\n",strlen(singleLine));
}
if (feof(file))
{
fclose(file);
}
else
{
printf("some other error occured");
}
}
else
{
printf("inside else\n");
}
return 0;
}