0

On the 1st part i'm reading the file and then I'm trying to write data into another txt file but was unable to run the file below ... the output error as below...please let me know...appreciate it very much....

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;
}
Doran L
  • 299
  • 2
  • 5
  • 19
  • These are compiler warnings not errors. – kaylum Nov 09 '15 at 03:01
  • @kaylum Compiler warnings can be turned into errors with the -Werror flag, I suspect this is the case here. It's quite common for the -Werror flag to be used since it forces the development of safer, more robust software. – Matt O Nov 09 '15 at 03:05
  • @MattO I'm well aware of that. But the output shown does not indicate that -Werror has been enabled. If it were it would not say "warning" it would say "error" and then "Warning as error". If you look through the SO archives you will find that there are many questions where new programmers think that the build failed when they see warnings. – kaylum Nov 09 '15 at 03:10
  • Fair call, I don't remember exactly how VS output looks when -Werror is used so I assumed that the "Error" in the output text was coming from VS, but perhaps it was added in by OP. – Matt O Nov 09 '15 at 03:14
  • which compiler are you using? Can you show the variable declaration part? add #define _CRT_SECURE_NO_WARNINGS at the beginig of your file( befor #include ), it might remove your error/warning, without harming your program. – Sigcont Nov 09 '15 at 04:23
  • So since you posted only 56 lines of source code I'm assuming those errors refer to a different file? That's obviously not going to get you any meaningful answers. – roeland Nov 09 '15 at 04:39
  • @roeland sorry there were some codes that i changed to comments... i updated all source codes – Doran L Nov 09 '15 at 05:01
  • Well, I'd say start with properly indenting your code. – roeland Nov 09 '15 at 05:30

2 Answers2

1

This might help you

I have used gcc to compile the code. There is an error..please check your do..while loop.... The following code compiler properly.

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

Please compile it once and let me know the error.(if any)

Community
  • 1
  • 1
Sigcont
  • 717
  • 2
  • 8
  • 16
0

The compiler is warning you not to use scanf("%s"), because it has serious security problems. It's also warning you not to use fopen, but fopen is perfectly safe as long as you check errno. They're recommending you use fopen_s instead, which is available in C11 only. It seems the main difference is that fopen_s returns it's error code instead of setting errno. That's not really a compelling reason to use it.

Community
  • 1
  • 1
Functino
  • 1,939
  • 17
  • 25