无法写入c中的文件

Unable to write to file in c

第一部分我正在读取文件然后我试图将数据写入另一个 txt 文件但无法 运行 下面的文件...输出错误如下.. .请告诉我...非常感谢....

Error output:
1>------ Build started: Project: DataValidation, Configuration: Debug Win32 ------
1>  Validate.cpp
1>d:\dropbox\validate.cpp(56): error C2059: syntax error : 'if'
1>d:\dropbox\validate.cpp(56): error C2143: syntax error : missing ';' before '{'
1>d:\dropbox\validate.cpp(58): error C2181: illegal else without matching if
1>d:\dropbox\validate.cpp(67): error C2059: syntax error : 'while'
1>d:\dropbox\validate.cpp(71): error C2065: 'inFile' : undeclared identifier
1>d:\dropbox\validate.cpp(71): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\dropbox\validate.cpp(71): error C2365: 'fclose' : redefinition; previous definition was 'function'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\stdio.h(184) : see declaration of 'fclose'
1>d:\dropbox\validate.cpp(73): error C2059: syntax error : 'return'
1>d:\dropbox\validate.cpp(74): error C2059: syntax error : '}'
1>d:\dropbox\validate.cpp(74): error C2143: syntax error : missing ';' before '}'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h> //for exit (1)

int main(){
FILE* inFile;   //file handle or pointer
FILE* outFile;
char there_was_error = 0;
char opened_in_read = 1;
char filename[100];
char line[100]; //buffer
char test;
int src, dest, type, port;
char data[100];

printf("Enter filename: ");
scanf("%s", filename);

printf("Processing filename %s ...\n", filename);
inFile = fopen(filename, "r");
if (inFile == NULL) {       //check if file handler is invalid
    printf("Could not open file %s\n", filename);
    exit(1);    //error status code
}

//test = fscanf(inFile, "%d:%d:%d:%d:", &src, &dest, &type, &port); //read line from file
//fgets(data, 100, inFile);
do {
    test = fscanf(inFile, "%d:%d:%d:%d:", &src, &dest, &type, &port);//read line from file
    fgets(data, 100, inFile);
    //printf("%d %d %d %d %s", src, dest, type, port, data);

    outFile = fopen("data_1.txt", "rb+");
    if (outFile == NULL)
    {
        opened_in_read = 0;
        outFile = fopen("data_1.txt", "wb");
        if (outFile == NULL)
            there_was_error = 1;
    }
    if (there_was_error)
    {
        printf("Disc full or no permission\n");
        return EXIT_FAILURE;
    }
    if (opened_in_read)
        printf("The file is opened in read mode."
        " Let's read some cached data\n");
    else
        printf("%d %d %d %d %s", src, dest, type, port, data);
    return EXIT_SUCCESS;
}


    if (src >= 1 && src <= 1024){
    }
    else {

    }
    if (dest >= 1 && dest <= 1024){
    }
    else {

    }

} while (test != EOF);



fclose(inFile); //must always close file once done

return 0;
}

编译器警告您不要使用 scanf("%s"),因为它有 serious security problems。它还警告您不要使用 fopen,但只要您检查 errnofopen 是绝对安全的。他们建议您改用 fopen_s,它仅在 C11 中可用。似乎主要区别在于 fopen_s returns 它是错误代码而不是设置 errno。这并不是使用它的真正令人信服的理由。

This might help you

我用gcc编译了代码。有错误..请检查你的do..while循环....以下代码编译正确。

    #define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h> //for exit (1)

int main(){
FILE* inFile;   //file handle or pointer
FILE* outFile;
char there_was_error = 0;
char opened_in_read = 1;
char filename[100];
char line[100]; //buffer
char test;
int src, dest, type, port;
char data[100];

printf("Enter filename: ");
scanf("%s", filename);

printf("Processing filename %s ...\n", filename);
inFile = fopen(filename, "r");
if (inFile == NULL) {       //check if file handler is invalid
    printf("Could not open file %s\n", filename);
    exit(1);    //error status code
}

//test = fscanf(inFile, "%d:%d:%d:%d:", &src, &dest, &type, &port); //read line from file
//fgets(data, 100, inFile);
do {
    test = fscanf(inFile, "%d:%d:%d:%d:", &src, &dest, &type, &port);//read line from file
    fgets(data, 100, inFile);
    //printf("%d %d %d %d %s", src, dest, type, port, data);

    outFile = fopen("data_1.txt", "rb+");
    if (outFile == NULL)
    {
        opened_in_read = 0;
        outFile = fopen("data_1.txt", "wb");
        if (outFile == NULL)
            there_was_error = 1;
    }
    if (there_was_error)
    {
        printf("Disc full or no permission\n");
        return EXIT_FAILURE;
    }
    if (opened_in_read)
        printf("The file is opened in read mode."
        " Let's read some cached data\n");
    else
        printf("%d %d %d %d %s", src, dest, type, port, data);
    return EXIT_SUCCESS;



    if (src >= 1 && src <= 1024){
    }
    else {

    }
    if (dest >= 1 && dest <= 1024){
    }
    else {

    }

} while (test != EOF);



fclose(inFile); //must always close file once done

return 0;
}

请编译一次并告诉我错误。(如果有)