Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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
我如何使用fopen;w+&引用;在c中写入和读取同一文件?_C - Fatal编程技术网

我如何使用fopen;w+&引用;在c中写入和读取同一文件?

我如何使用fopen;w+&引用;在c中写入和读取同一文件?,c,C,我正在研究如何使用C编写和读取文件。在发现使用usign fopen(“foo”,“r+”)或fopen(“foo”,“w+”)应该是可能的之后,我决定创建一个虚拟程序来尝试一下。 以下是节目: #include <stdio.h> int main(){ char reader[81]; FILE *USE = fopen("dummy.txt", "w+"); fprintf(USE,"whatever \r\nhello"); while(fge

我正在研究如何使用C编写和读取文件。在发现使用usign fopen(“foo”,“r+”)或fopen(“foo”,“w+”)应该是可能的之后,我决定创建一个虚拟程序来尝试一下。 以下是节目:

#include <stdio.h>
int main(){
    char reader[81];
    FILE *USE = fopen("dummy.txt", "w+");
    fprintf(USE,"whatever \r\nhello");
    while(fgets(reader, 80, USE)) {
        printf("%s", reader);
    }
    fclose(USE);
    return 0;
}
#包括
int main(){
字符读取器[81];
文件*USE=fopen(“dummy.txt”,“w+”);
fprintf(使用“whatever\r\nhello”);
while(fgets(读卡器,80,使用)){
printf(“%s”,读卡器);
}
fclose(使用);
返回0;
}
其想法是创建一个名为dummy.txt的新文件。每次执行此程序时,都会在dummy.txt中写入2行,然后在命令行或终端中显示这些行。正如你所看到的,这并不是最有用的程序,但是知道如何做到这一点在将来会非常有用


任何帮助都很好

您忘了倒带文件:

fprintf(USE,"whatever \r\nhello");

rewind(USE);   //<<< add this

while(fgets(reader, 80, USE)) {
fprintf(使用“whatever\r\nhello”);

倒带(使用)// 要再次读取文件,您应使用
倒带
功能:

 fprintf(USE,"whatever \r\nhello");

 rewind(USE); // put the file pointer to the begin of file;

 while(fgets(reader, 80, USE));
另一种方法是使用
fseek

int fseek(FILE *stream, long int offset, int whence)
其中:

  • 溪流− 这是指向文件对象的指针,该文件对象标识 小溪

  • 抵消− 这是要从何处偏移的字节数

  • 从哪里来的− 这是添加偏移的位置。它是
    由以下常量之一指定:

    1 - SEEK_SET    Beginning of file
    2 - SEEK_CUR    Current position of the file pointer
    3 - SEEK_END    End of file
    
您还可以使用:

long int ftell ( FILE * stream ); 
用于流中的当前位置


,有关fseek vs revend的更多信息

我的解决方案,不要使用w+,而是使用r+

文件顶部

#include <stdio.h>
#include <stdlib.h>
int openFile() {
    g_file = fopen("my_file.txt", "r+");
    if( g_file == NULL )
    {
        // No file ?
        g_file = fopen("my_file.txt", "w+");
    }
    return 0;
}
#define MAX 64
#define true  1
#define false 0

int writeReadFile() {
    char line[MAX] = "";
    int size = 0;

    while( fgets(line, MAX, g_file) )
    {
        // Check something
        // result = sscanf(line, etc...)
        // if ( result == argument number ok) ...
        // strcmp( ? , ? ) == 0
        // strlen compare size of the previous line and new line   
        if ( true ) {
            size = ftell(g_file);
        }

        // Replace something
        if( true ) {
            fseek(g_file, size, SEEK_SET);            
            // fputc if size differ
            // fprintf if size ok
        }
        size = ftell(g_file);
    }
    fprintf(g_file, "%s\n", "info");
}
打开文件

#include <stdio.h>
#include <stdlib.h>
int openFile() {
    g_file = fopen("my_file.txt", "r+");
    if( g_file == NULL )
    {
        // No file ?
        g_file = fopen("my_file.txt", "w+");
    }
    return 0;
}
#define MAX 64
#define true  1
#define false 0

int writeReadFile() {
    char line[MAX] = "";
    int size = 0;

    while( fgets(line, MAX, g_file) )
    {
        // Check something
        // result = sscanf(line, etc...)
        // if ( result == argument number ok) ...
        // strcmp( ? , ? ) == 0
        // strlen compare size of the previous line and new line   
        if ( true ) {
            size = ftell(g_file);
        }

        // Replace something
        if( true ) {
            fseek(g_file, size, SEEK_SET);            
            // fputc if size differ
            // fprintf if size ok
        }
        size = ftell(g_file);
    }
    fprintf(g_file, "%s\n", "info");
}
读写

#include <stdio.h>
#include <stdlib.h>
int openFile() {
    g_file = fopen("my_file.txt", "r+");
    if( g_file == NULL )
    {
        // No file ?
        g_file = fopen("my_file.txt", "w+");
    }
    return 0;
}
#define MAX 64
#define true  1
#define false 0

int writeReadFile() {
    char line[MAX] = "";
    int size = 0;

    while( fgets(line, MAX, g_file) )
    {
        // Check something
        // result = sscanf(line, etc...)
        // if ( result == argument number ok) ...
        // strcmp( ? , ? ) == 0
        // strlen compare size of the previous line and new line   
        if ( true ) {
            size = ftell(g_file);
        }

        // Replace something
        if( true ) {
            fseek(g_file, size, SEEK_SET);            
            // fputc if size differ
            // fprintf if size ok
        }
        size = ftell(g_file);
    }
    fprintf(g_file, "%s\n", "info");
}
主菜单

int main(void)
{
    printf("Hello World !\n");

    openFile();
    writeReadFile();

    return 0;
}
请不要删除我的帖子。 有一次我写了这个。我从页面接收链接,保存它,我可以将其重新用于其他代码。所以,不要删除。多谢各位

他在我这边编译,也希望你。
请删除,不要放不好的笔记。

非常感谢您的回答,这比我想要的要多得多:):)这不可编译。另外,您说您不应该使用
w+/code>,但如果您的建议(使用
r+/code>)失败,并且您甚至没有显示如何声明
大小,您仍然可以继续使用它。记住,质量差的答案可能会被删除。如果您担心您的答案可能会被删除,那么它可能有问题。