Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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 这段代码每保存一个.txt文件的第三个字符有什么问题?_C - Fatal编程技术网

C 这段代码每保存一个.txt文件的第三个字符有什么问题?

C 这段代码每保存一个.txt文件的第三个字符有什么问题?,c,C,我正试图编写一个程序,保存.txt文件的每三个字符。此处使用的文本文件名为Hello.txt,如下所示: ABCDEFGHIJKLMNOPQRSTUVWXYZ 以下是我尝试过的代码: #pragma warning(disable:4996) #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char ** argv) { int ch;

我正试图编写一个程序,保存.txt文件的每三个字符。此处使用的文本文件名为Hello.txt,如下所示:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

以下是我尝试过的代码:

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char ** argv)
{
    int ch;
    int count = 0;
    char name[20];
    FILE *fp, *out;

    fp = fopen(argv[1], "r");  // argv[1] is "Hello.txt" 

    strncpy(name, argv[1], 5);  // Just copying "Hello"
    name[5] = '\0';              
    strcat(name, "red");        // Adding "red" to "Hello"

    out = fopen(name, "w");     // File pointer to "Hellored"

    while( ( ch=getc(fp) ) != EOF)  // Copying content of file "Hello.txt"
        if (count % 3 == 0)         // Only want to save every third character of "Hello.txt"
        {
            putc(ch, out);
            count++;
        }

}
正如预期的那样,它将Hellored文件保存在工作区中。但是当我在文件末尾添加.txt打开它时,输出是A。这段代码有什么问题

    if (count % 3 == 0)         // Only want to save every third character of "Hello.txt"
    {
        putc(ch, out);
        count++;
    }
你只能增加一次计数

循环计数的第一次为0,因此执行if体

在所有后续时间,count为1,if体不再执行,count也不再递增

将增量移到if外部

你只能增加一次计数

循环计数的第一次为0,因此执行if体

在所有后续时间,count为1,if体不再执行,count也不再递增

将增量移到if之外。

您没有正确增加计数。它需要为每个字符递增,而不仅仅是在if块内

其他清理:

添加代码以在函数结束前关闭文件

fclose(out);
fclose(fp);
您没有正确增加计数。它需要为每个字符递增,而不仅仅是在if块内

其他清理:

添加代码以在函数结束前关闭文件

fclose(out);
fclose(fp);
不要在IFT语句中增加计数

while( ( ch=getc(fp) ) != EOF) { //make sure you have {} around your while block
    if (count % 3 == 0) {
        putc(ch, out);
    }
    count++;
}
不要在IFT语句中增加计数

while( ( ch=getc(fp) ) != EOF) { //make sure you have {} around your while block
    if (count % 3 == 0) {
        putc(ch, out);
    }
    count++;
}
在代码中,如果count==0,则变量count只增加一次。因此if语句只会变为true一次,并且在该计数之后始终为1

你必须在程序结束时用fclosefp和fcloseout关闭文件, 为确保所有数据都写入该文件

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char ** argv)
{
    int ch;
    int count = 0;
    char name[20];
    FILE *fp, *out;

    fp = fopen(argv[1], "r");  // argv[1] is "Hello.txt" 

    strncpy(name, argv[1], 5);  // Just copying "Hello"
    name[5] = '\0';              
    strcat(name, "red");        // Adding "red" to "Hello"

    out = fopen(name, "w");     // File pointer to "Hellored"

    while( ( ch=getc(fp) ) != EOF)  // Copying content of file "Hello.txt"
        if (count++ % 3 == 0)         // Only want to save every third character of "Hello.txt"
        {
            putc(ch, out);
        }
    fclose(out);
    fclose(fp);
}
在代码中,如果count==0,则变量count只增加一次。因此if语句只会变为true一次,并且在该计数之后始终为1

你必须在程序结束时用fclosefp和fcloseout关闭文件, 为确保所有数据都写入该文件

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char ** argv)
{
    int ch;
    int count = 0;
    char name[20];
    FILE *fp, *out;

    fp = fopen(argv[1], "r");  // argv[1] is "Hello.txt" 

    strncpy(name, argv[1], 5);  // Just copying "Hello"
    name[5] = '\0';              
    strcat(name, "red");        // Adding "red" to "Hello"

    out = fopen(name, "w");     // File pointer to "Hellored"

    while( ( ch=getc(fp) ) != EOF)  // Copying content of file "Hello.txt"
        if (count++ % 3 == 0)         // Only want to save every third character of "Hello.txt"
        {
            putc(ch, out);
        }
    fclose(out);
    fclose(fp);
}

将count++放在IFL之外,请记住丢失文件。同时检查fopen呼叫是否成功。感谢您提供了。@RSahu哈哈谢谢R Sahu!如果没有充分的理由,不应仅在本地禁用警告。你在这里具体禁止什么?删除该pragma,并在代码中用注释标记该位置。将count++放在ifremote to fclose文件之外。同时检查fopen呼叫是否成功。感谢您提供了。@RSahu哈哈谢谢R Sahu!如果没有充分的理由,不应仅在本地禁用警告。你在这里具体禁止什么?删除该pragma,并在代码中用注释对该位置进行警告标记。