2015-11-09 32 views
0

在第一部分我正在讀取文件,然後我試圖寫入數據到另一個txt文件,但無法運行下面的文件...輸出錯誤如下。 ..please讓我知道......非常感激....無法寫入文件在c

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; 
} 
+0

這些編譯器警告不是錯誤。 – kaylum

+0

@kaylum編譯器警告可以通過-Werror標誌變成錯誤,我懷疑這是這種情況。由於它會強制開發更安全,更強大的軟件,所以使用-Werror標誌是很常見的。 –

+0

@MOOTO我很清楚這一點。但是顯示的輸出並不表示 - 已經啓用了錯誤。如果是這樣,它不會說「警告」,它會說「錯誤」,然後「警告爲錯誤」。如果你仔細看看SO檔案,你會發現新程序員在看到警告時認爲構建失敗了很多問題。 – kaylum

回答

1

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; 
} 

請編譯一次讓我知道了錯誤。(如果有的話)

+0

感謝它的工作正常...會小心while while循環 –

0

編譯器警告您不要使用scanf("%s"),因爲它有serious security problems。它也警告您不要使用fopen,但只要您檢查errno,但fopen是完全安全的。他們建議您使用fopen_s代替,僅在C11中提供。看起來主要區別在於fopen_s返回錯誤代碼而不是設置errno。這不是一個真正令人信服的理由來使用它。