C文件指针返回文件内容两次

C文件指针返回文件内容两次,c,C,使用下面的程序,我希望读取一个文件(比如,一个文本文件)并将其所有内容存储在一个变量中。因此,为了实现它,我在堆栈溢出建议的帮助下编写了以下内容。但是,该程序会两次返回文件的内容。 例如,让以下程序读取包含以下内容的文本文件: 约翰先发0分 使用*,15 然后,程序将显示以下内容: 约翰先发0分 使用*,15 约翰先发0分 使用*,15 因此,我希望你能帮助我找出问题所在。提前多谢 //Program to read a file and store it's contents

使用下面的程序,我希望读取一个文件(比如,一个文本文件)并将其所有内容存储在一个变量中。因此,为了实现它,我在堆栈溢出建议的帮助下编写了以下内容。但是,该程序会两次返回文件的内容。 例如,让以下程序读取包含以下内容的文本文件:

  • 约翰先发0分
  • 使用*,15
然后,程序将显示以下内容:

  • 约翰先发0分
  • 使用*,15
  • 约翰先发0分
  • 使用*,15
因此,我希望你能帮助我找出问题所在。提前多谢

    //Program to read a file and store it's contents in a single variable
    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    #include<stdlib.h>

    /*Reads contents of a file and returns address of variable, in which file contents are stored*/
    char* fileRead(char*);
    //Concatenates 2 strings
    char* concat(char*,char*);  
    //Calculates the size of the file
    unsigned long fsize(char* file);

    int main()
    {

      char *prt, *out;
      //Allocate memory & take the input of the file name to read
      prt = malloc(256);
      printf("\nEnter the name of the file : \t");
      scanf("%255s",prt);
      //Copy the address of read data & output it
      out = fileRead(prt);
      printf("\nContents : \n-----------------\n%s", out);

      free(out);    
      free(prt);
      return 0;
    }

    char* fileRead(char *file)
    {   
      //function to read the contents of a file

      FILE  *fip;
      char *text, *temp, read[1024];
      int size, i=0;

      fip=fopen(file, "r");
      size=(int)fsize(file);

      temp = malloc(size*10);
      text = temp;
      //If the file doesn't exist then...
      if(fip==NULL)
      {
        temp = strdup("ERROR : File doesn't exist! Please try again!\n");
        return temp;
      } 
      //Begin concatenation, once after the file reading has been initiated
      while(fgets(read, 1024, fip) != NULL)
      {
        temp = concat(text,read);i++;
      }

      fclose(fip);

      return text;
     }

     char* concat(char* dest, char* src)
     {  
      //function to concatenate src to dest
      while(*dest) dest++;  
      while(*dest++ = *src++);
      return --dest;
     }

     unsigned long fsize(char* file)
     {  
      //calculates the size of file
      FILE * f = fopen(file, "r");
      fseek(f, 0, SEEK_END);
      unsigned long len = (unsigned long)ftell(f);
      fclose(f);
      return len;
     }
//读取文件并将其内容存储在单个变量中的程序
#包括
#包括
#包括
#包括
/*读取文件内容并返回存储文件内容的变量地址*/
字符*文件读取(字符*);
//连接2个字符串
char*concat(char*,char*);
//计算文件的大小
无符号长fsize(char*文件);
int main()
{
字符*prt,*out;
//分配内存&输入要读取的文件名
prt=malloc(256);
printf(“\n输入文件名:\t”);
扫描频率(“%255s”,prt);
//复制读取数据的地址并将其输出
out=文件读取(prt);
printf(“\n内容:\n-------------\n%s”,输出);
免费(外出);
免费(prt);
返回0;
}
char*fileRead(char*file)
{   
//函数读取文件的内容
文件*fip;
字符*文本,*温度,读取[1024];
整数大小,i=0;
fip=fopen(文件“r”);
size=(int)fsize(文件);
温度=malloc(尺寸*10);
文本=温度;
//如果文件不存在,则。。。
如果(fip==NULL)
{
temp=strdup(“错误:文件不存在!请重试!\n”);
返回温度;
} 
//启动文件读取后,立即开始连接
while(fgets(读取,1024,fip)!=NULL)
{
temp=concat(文本,读取);i++;
}
fclose(fip);
返回文本;
}
char*concat(char*dest,char*src)
{  
//函数将src连接到dest
while(*dest)dest++;
而(*dest++=*src++);
返回——dest;
}
无符号长fsize(char*文件)
{  
//计算文件的大小
文件*f=fopen(文件“r”);
fseek(f,0,SEEK_END);
无符号长len=(无符号长)ftell(f);
fclose(f);
回程透镜;
}

编辑1:非常感谢大家。为您提供快速响应和高效答案。至于大小*10的问题,我想出了一个随机的主意,来处理一个分割错误。从未想过大小+1选项。我从你们身上学到了很多。很快就会有一个新问题。再次感谢

您的问题是您使用“文本”作为字符串,但未能做到这一点。 C中的“字符串”是以“\0”结尾的字符数组

如果您使用任何与字符串相关的函数,您必须确定您是否给出了字符串! 因此,在分配了“text”(通过“temp”)之后,“text”还不是字符串,因为它不包含“\0”。 因为它在开始时是一个“空”字符串,所以必须通过执行

text[0] = '\0';

现在,为什么文件要打印两次?不知道,但我必须赌UB,因为您的字符串没有正确初始化。

欢迎使用堆栈溢出!听起来您可能需要学习如何使用a来逐步完成代码。有了一个好的调试器,您可以逐行执行您的程序,并查看它偏离预期的地方。这是一个必要的工具,如果你要做任何编程。进一步阅读:。我很难理解“temp”变量背后的逻辑。得到了“temp”变量背后的逻辑。不错,但是变量名一点用处都没有,而且因为没有注释,也没有说明temp是如何在代码中使用的,所以很难理解其逻辑。“temp”是“endOfCurrentString”,用于快速连接。OP只是没能正确地使用它。还有,你为什么要将所需的空间缩短十倍?为什么不只是“temp=malloc(size+1);”?uhhh,fgets将读取到size-1,或者如果他发现了换行符('\n'),或者显然是EOF。我想你是说弗瑞德?