Devc++ and MS c++ file i/o

This is a discussion on Devc++ and MS c++ file i/o within the C Programming forums, part of the General Programming Boards category; when I compile the following code in Dev c++ it prints correct to the file. But when I run it ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    22

    Devc++ and MS c++ file i/o

    when I compile the following code in Dev c++ it prints correct to the file. But when I run it in MS Visual c++6.0 it creates the folder but does not prints anything to the folder why is this. I have my project due in MS software. Please help.Thanks in Advance

    Code:
    #include <stdio.h>
    int logfile();
    int main(){
        
    FILE * ptr;
    ptr=fopen("C:\\IN.txt", "w");
    	fprintf(ptr, "& START ");
    	
    	logfile();getchar();
    	getchar();
    }
    int logfile(){
    
    	FILE *ptr;
    	ptr=fopen("C:\\IN.txt", "w");
    	fprintf(ptr, " goes &START\n ");
    
    
    
    }

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Code:
    #include <stdio.h>
    int logfile();
    int main(){
            FILE * ptr;
            ptr=fopen("C:\\IN.txt", "w");
    	fprintf(ptr, "& START ");
    	logfile();
            getchar();
    	getchar();
    }
    int logfile(){
    	FILE *ptr;
    	ptr=fopen("C:\\IN.txt", "w");
    	fprintf(ptr, " goes &START\n ");
    }
    You are opening C:\IN.txt twice. You do not need to open it the second time. And you dont need to declare *ptr again. Logfile doesn't return an int. You have it declared as returning an int but then it never returns anything. Try this-
    Code:
    #include <stdio.h>
    
    void logfile(FILE *fp);
    
    int main(){
                 FILE *ptr;
                 ptr=fopen("C:\\IN.txt","w");
                 fprintf(ptr,"& START");
                 logfile(ptr);
                 getchar();
                 getchar();
    }
    
    void logfile(FILE *fp){
            fprintf(fp," goes & START\n");
    }
    I just didn't declare logfile as returning an int and made it take a file pointer as an argument. It takes that file handle and prints into it.

    It works fine for me but I am using Dev-C++. Try it on MSVC++ though.
    ~Sven
    Last edited by 00Sven; 04-02-2006 at 07:17 PM.
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    THIS IS WEIRED it worked in DEV c++ as you said. But MS VISUAL STUDIO JUST CREAT THE FOLDER BUT DIDN'T PRINT ANYTHING> I WONDER IF THERE IS A SPECIFIC WAY I should do this in MS C++

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    Btw Thankx For The Reply. Any Help Would Be Great

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    Microsoft.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by ypramesh
    Microsoft.
    Microsoft what?

    Maybe you should try closing the file before calling logfile and close it again after? Maybe?

    - xeddiex

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    thanx for reply. Microsoft?? nah I am just tired and trying blame the big guy. i am sure i messed up somthing

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    Code:
    fptr=fopen("C:\\IN.txt","w");
    fprintf(fptr,"& START");
    int fclose(FILE *fptr);
    will that work?? Thankx

  9. #9
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    That won't work. That is attempting to declare a file handle as an argument of fclose. Try closing the file with fclose after you call logfile(). That might work I am not sure.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    its printing to the file after i put fclose at the end of logfile. but in my actual code i have repeated calls to logfile. but only the last one get prited in the file. So it's deleting the previous one and prints the new one to the file. is that because i have "w" type access to file. how should i fix this. Thankx

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by ypramesh
    its printing to the file after i put fclose at the end of logfile. but in my actual code i have repeated calls to logfile. but only the last one get prited in the file. So it's deleting the previous one and prints the new one to the file. is that because i have "w" type access to file. how should i fix this. Thankx
    C File I/O:
    r - open for reading
    w - open for writing (file need not exist)
    a - open for appending (file need not exist)
    r+ - open for reading and writing, start at beginning
    w+ - open for reading and writing (overwrite file)
    a+ - open for reading and writing (append if file exists)

    - xeddiex

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21