I have this function that should open two files and write their content in a third file, separated by "\n". I don't get why it works for code in comments and not for the one below it, although both provide valid text file names.

void concatenate()
{
    /*char *file=new char[strlen(" ")+strlen(mFile)];
        strcpy(file,"\0");
        file=strcat(file," ");
        file=strcat(file,mFile);*/

        char *file=new char[strlen("aux.txt")];
        strcpy(file,"aux.txt");

        FILE *f=fopen(file,"w");
        FILE *f1=fopen(F.getFile(),"r");
        FILE *f2=fopen(mFile,"r");

        char s[100];

        while (!feof(f1))
        {
            fgets(s,100,f1);
            fseek(f,0,SEEK_END);
            fprintf(f,s,"%s");
        }
        fclose(f1);

        fprintf(f,"\n","%s");

        while (!feof(f2))
        {
            fgets(s,100,f2);
            fseek(f,0,SEEK_END);
            fprintf(f,s,"%s");
        }
        fclose(f2);
        fclose(f);
}

lines 8 and 9 are unnecessary. Just write line 11 like this
FILE *f=fopen("aux.txt","w");

line 12: What is variable F?

line 13: What is mfile?

line 17: Don't use feof(). and fseek() is not needed either. Also the parameters are incorrect order for fprintf()

  while (fgets(s,100,f2) )
  {
      fprintf(f,"%s",s);
  }

Actually, concatenate is an overloaded + operator. I wanted to create separate files for every object that is resulted from calling it, by concatenating something("aux") to one of the objects' filename.

F is an object and mFile it's one of his data members, a char which retains the name of the file.

I changed fprintf's parameters' order, and eliminated the feof() and fseek() functions. But i still get debug assertion failed. I would've thought it's something wrong with my classes' implementations, but it break when i call fprintf() for first time(line 21).

Are you still using line 8? If you are then it isn't allocating enough memory to hold the filename plus string's null terminating byte. Otherwise the only other thing I can suggest is for you to learn how to use your compiler's debugger and single-step through all the lines of that function to find out what it's doing.

Changed line 8.

char *file=new char[strlen("aux.txt")+1];

I do know how to use the debugger. Everything works until line 21.

repost current code please.

    void concatenate()
    {
        /*char *file=new char[strlen(" ")+strlen(mFile)];
            strcpy(file,"\0");
            file=strcat(file," ");
            file=strcat(file,mFile);*/
            char *file=new char[strlen("aux.txt")+1];
            strcpy(file,"aux.txt");

            FILE *f=fopen(file,"w");
            FILE *f1=fopen(F.getFile(),"r");
            FILE *f2=fopen(mFile,"r");

            char s[100];

            while (fgets(s,100,f1))
                fprintf(f,"%s",s);       //here it stops

            fclose(f1);

            fprintf(f,"%s","\n");

            while (fgets(s,100,f2))
                fprintf(f,"%s",s);

            fclose(f2);
            fclose(f);
    }

The only thing I see that might cause the problem is if fopen on line 10 failed. You might put in checks after lines 10, 11 and 12 to make sure the files are opened successfully.

Indeed, i get that "aux.txt" does not exist. But isn't it supposed to create it when i open it and choose the option "w"?

Check your errors. For example, whenever opening a file fopen() returns NULL on failure:

FILE *f=fopen(file,"w");

if (f == NULL) {
    perror("Error opening output file");

    /* Whatever exit or recovery strategy you choose here */
    exit(EXIT_FAILURE);
}

If nothing else you may get more useful information in the way of first knowing that fopen() failed and also what the exact error was.

It prints out "Error opening file. No such file or directory."

Thy this code:

    void concatenate()
    {
            FILE *f=fopen("aux.txt","w");
            if( f == NULL)
            {
                printf("Error opening aux.txt file\n");
                 exit(EXIT_FAILURE);
            }
            FILE *f1=fopen(F.getFile(),"r");
            if( f1 == NULL)
            {
                printf("Error opening '%s' file\n", F.getFile());
                 exit(EXIT_FAILURE);
            }
            FILE *f2=fopen(mFile,"r");
            if( f2 == NULL)
            {
                printf("Error opening '%s' file\n", mfile);
                 exit(EXIT_FAILURE);
            }

            char s[100];

            while (fgets(s,100,f1))
                fprintf(f,"%s",s);       //here it stops

            fclose(f1);

            fprintf(f,"%s","\n");

            while (fgets(s,100,f2))
                fprintf(f,"%s",s);

            fclose(f2);
            fclose(f);
    }
void concatenate()
{
    /*char *file=new char[strlen(" ")+strlen(mFile)];
    strcpy(file,"\0");
    file=strcat(file," ");
    file=strcat(file,mFile);*/
    char *file=new char[strlen("aux.txt")+1];
    strcpy(file,"aux.txt");

    FILE *f=fopen(file,"w");
    FILE *f1=fopen(F.getFile(),"r");
    FILE *f2=fopen(mFile,"r");

    if (f==NULL)
    {
        //throw new CExceptie("Error opening file.");
        perror("Error opening file.");
        exit(EXIT_FAILURE);
        getch();
    }

    if (f1==NULL)
        throw new CExceptie("File f1 does not exist!");

    if (f2==NULL)
        throw new CExceptie("File f2 does not exist!");

    char s[100];

    while (fgets(s,100,f1))
        fprintf(f,"%s",s);

    fclose(f1);

    fprintf(f,"%s","\n");

    while (fgets(s,100,f2))
        fprintf(f,"%s",s);

    fclose(f2);
    fclose(f);
}

I don't get what's different.. Besides the order of the lines.

Did you try the code I posted? If yes, did it work or not?

I did try. I still get "Error opening aux.txt file"..

Do you have permissions to write to the folder? Are you using *nix or MS-Windows? Check the permissions of the folder which you are attempting to write the file to. Another hint is to turn off anitivirus program to see if that fixes the problem.

I'm using MS-Windows. It couldn't be the permissions (it worked for the file name obtained by concatenation of the 2 file names). Trying to create the file manually i get the following error message:

Items and files cannot:
- contain of the following characters: ?:&*"<>|#%
- contain control Unicode characters;
- contain surrogate control characters
- be system reserved names, including 'CON','AUX','PRN','COM1','PRN2'
- be '.' or '..'

Please enter a valid name.

Now it still doesn't work, but i assume it's a problem of Microsoft Visual Studio. It says the file in which i want to write does not exist, but when i try to add it manually it says file already exists.

Well in the warning you got it says the file name can't be "aux" and you are using that. Try chnaging the name of your file to something else.

I just tried it using VC++ 2012 on Windows 8. This is what happened:

File name either "aux.txt" or "aux" (no extension). Neither file name was created, but fopen() succeeded and no errors writing to the file.

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

int _tmain(int argc, _TCHAR* argv[])
{
    FILE* fp = fopen("aux.txt","w");
    if( fp == NULL)
    {
        printf("Error\n");
        return 1;
    }
    fprintf(fp,"Hello World\n");
    fclose(fp);
    printf("All done\n");
    return 0;
}

I have VC++ 2010.

When I previously stated that it still didn't work i have already tried another names than "aux". But I was getting same "file does not exist" error for the file I was creating using fopen with "w" option.

I commented the part where I was checking if (f==NULL) and it works fine now. Then I checked the folder where the files are created for the current project and the files with the names I tried after replacing "aux" where there and their content was the desired one.(so the compiler was right when it was telling "file already created" when i was trying to manually add the text file).

Conclusion:

Do not check if (f==NULL) after FILE *f=fopen(filename,"w").

Do not check if (f==NULL) after FILE *f=fopen(filename,"w").

Wrong conclusion. You can't use file name aux because aux is a reserved system name, as previously mentioned by Nathan.

When I previously stated that it still didn't work i have already tried other names than "aux". But I was getting same "file does not exist" error for the file I was creating using fopen with "w" option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.