Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我在文件中写入内容时,它会在C中的文件顶部添加空行。如何修复它 #包括 #包括 void main() { 文件*文件; int size=0,len,i=0; char*str,str2,文件名[20]; printf(“输入文件名:”); scanf(“%s”,文件名); //------------------用于在文件中写入数据------------------// file=fopen(文件名,“w”); //fseek(文件,0L,2); printf(“输入数据:”); 而((str2=getchar())!=27) { putc(str2,文件); } fclose(文件); //------------------用于读取文件中的数据------------------// file=fopen(文件名为“r”); fseek(文件,0L,2); len=ftell(文件); 尺寸=透镜; fseek(文件,0L,0); str=(char*)malloc(len*sizeof(char)); 而((str2=getc(文件))!=EOF) { str[i]=str2;i++; } printf(“\n文件数据:-\n”); 对于(i=0;i_C - Fatal编程技术网

当我在文件中写入内容时,它会在C中的文件顶部添加空行。如何修复它 #包括 #包括 void main() { 文件*文件; int size=0,len,i=0; char*str,str2,文件名[20]; printf(“输入文件名:”); scanf(“%s”,文件名); //------------------用于在文件中写入数据------------------// file=fopen(文件名,“w”); //fseek(文件,0L,2); printf(“输入数据:”); 而((str2=getchar())!=27) { putc(str2,文件); } fclose(文件); //------------------用于读取文件中的数据------------------// file=fopen(文件名为“r”); fseek(文件,0L,2); len=ftell(文件); 尺寸=透镜; fseek(文件,0L,0); str=(char*)malloc(len*sizeof(char)); 而((str2=getc(文件))!=EOF) { str[i]=str2;i++; } printf(“\n文件数据:-\n”); 对于(i=0;i

当我在文件中写入内容时,它会在C中的文件顶部添加空行。如何修复它 #包括 #包括 void main() { 文件*文件; int size=0,len,i=0; char*str,str2,文件名[20]; printf(“输入文件名:”); scanf(“%s”,文件名); //------------------用于在文件中写入数据------------------// file=fopen(文件名,“w”); //fseek(文件,0L,2); printf(“输入数据:”); 而((str2=getchar())!=27) { putc(str2,文件); } fclose(文件); //------------------用于读取文件中的数据------------------// file=fopen(文件名为“r”); fseek(文件,0L,2); len=ftell(文件); 尺寸=透镜; fseek(文件,0L,0); str=(char*)malloc(len*sizeof(char)); 而((str2=getc(文件))!=EOF) { str[i]=str2;i++; } printf(“\n文件数据:-\n”); 对于(i=0;i,c,C,您的程序基本上以文件名作为输入,然后继续扫描输入数据,直到遇到Esc。此程序的第一部分很重要,逻辑与此类似 #include <stdio.h> #include <stdlib.h> void main() { FILE *file; int size=0,len,i=0; char *str,str2,filename[20]; printf("enter the filename :");

您的程序基本上以文件名作为输入,然后继续扫描输入数据,直到遇到Esc。此程序的第一部分很重要,逻辑与此类似

#include <stdio.h>
#include <stdlib.h>
void main()
{

        FILE *file;

        int size=0,len,i=0;

        char *str,str2,filename[20];

        printf("enter the filename :");
        scanf("%s",filename);

        //------------------For Write data in File------------------//
        file = fopen(filename,"w");
        //fseek(file,0L,2); 
        printf("input data:");
        while((str2=getchar())!= 27)
        {
            putc(str2,file);
        }
        fclose(file);

        //------------------For Read data in File------------------//
        file = fopen(filename,"r");
        fseek(file,0L,2);
        len = ftell(file);
        size=len;
        fseek(file,0L,0);

        str=(char *)malloc(len *sizeof(char));
        while((str2=getc(file))!= EOF)
        {
            str[i]=str2;i++;
        }
        printf("\nFile Data:-\n");
        for(i=0;i<size;i++)
        {
            printf("%c",str[i]);
        }
        fclose(file);
        printf("\n"); 
}
scanf手册上说

s匹配一系列非空白字符;下一个 指针必须是指向字符数组的指针,其长度足以 保留输入序列和终止的空字符('\0'), 自动添加。输入字符串在空白处停止 或在最大字段宽度处,以先到者为准

这意味着所有内容都会被扫描到字符缓冲区中,直到新的行字符在输入缓冲区中保持不变

当getchar()为时,在while循环中看到这个剩余的“\n”将扫描这个字符并继续写入文件

   char fname[20]; // Buffer to store File-Name
   scanf("%s", fname); 
因此

行中的第一个字符变为行

从输入缓冲区中删除不必要的字符

while((str2=getchar())!= 27)
    {
        putc(str2,file);
    }

我明白,但是,我能做些什么来改变代码。但是你能详细解释一下实际发生了什么吗?提前
printf("enter the filename :");
scanf("%s%*c",filename);