Print to file

This is a discussion on Print to file within the C Programming forums, part of the General Programming Boards category; Greetings all, Any idea on the following code? This code is out of a beginners 'C' book; at first I ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Maryland
    Posts
    13

    Print to file

    Greetings all,

    Any idea on the following code? This code is out of a beginners 'C' book; at first I got the error message, then I fixed the file location address and the error message went away. Now when I check the file for content, there is nothing.

    Note: I'm using Borland Turbo C++ Ver. 3.0

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    FILE *fptr;
    
    main()
    {
       int age = 22;	/* Simple variables to write */
    
       float salary = 19670.50;
    
       fptr = fopen("C:\WINDOWS\DESKTOP\BT\MYDATA.TXT", "w");  /* Opens for output */
    
       if (fptr == 0)
    	{ printf("An error occurred while opening the file.\n");
    	  exit (1);
    	}
    
       fprintf(fptr, "Here is some stuff:\n");
    
       fprintf(fptr, "I am %d years old.\n", age);
    
       fprintf(fptr, "I make $%.2f dolLars every three months!\n", salary);
    
       fclose(fptr);	/* Always close your files */
    
    return 0;
    }

    Eating the elephant one byte at a time.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    I find it hard to believe that the creation of that file succeeded. You would need to use a slash (/) or double backslashes (\\).

    Apart from that, I can't see anything wrong with your code.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    "C:\WINDOWS\DESKTOP\BT\MYDATA.TXT"
    You need to escape the backslashes.
    "C:\\WINDOWS\\DESKTOP\\BT\\MYDATA.TXT"

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Or use slashes (easier to read, imho):
    Code:
    "C:/windows/desktop/bt/mydata.txt"

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Maryland
    Posts
    13
    Got it! Get rid of the back slashes. Thanks, I'll fix and try again when I get to my home computer.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Maryland
    Posts
    13
    For cwr,

    I'm sorry, but didn't mention that I had already created the 'mydata' file prior to the program run.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It wouldn't have mattered if you had created it. Your inital string was incorrect, so rather than it opening the file you had made, it would have been trying to create a different one, because the escape codes were not valid codes. It wouldn't translate to the "correct" path to the file you had made. That was cwr's point.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Location
    Maryland
    Posts
    13
    For info, this code is basically out of ' The absolute beginners guide to C' second edition a SAM's publication pg# 312. As originally written the code gave me an error message. The address was fixed by making it all capitals and changing C:\\ to C:\, then the error message went away.

  9. #9
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by metaTron
    For info, this code is basically out of ' The absolute beginners guide to C' second edition a SAM's publication pg# 312. As originally written the code gave me an error message. The address was fixed by making it all capitals and changing C:\\ to C:\, then the error message went away.
    I don't have a copy of that book, but changing C:\\ to C:\ would not fix anything, it would only break things. What was the original error message that you got?

  10. #10
    Registered User
    Join Date
    Jan 2006
    Location
    Maryland
    Posts
    13
    The original error message was the one that was supposed to happen,
    Code:
    if (fptr == 0)
    { printf("An error occurred while opening the file.\n");
    	  exit (1);
    	}

  11. #11
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    You probably should have paid attention to that error.

    You should also have your code display why it failed. Look up the strerror function and errno.

  12. #12
    Registered User
    Join Date
    Jan 2006
    Location
    Maryland
    Posts
    13
    Got it, this gives me some things to look at tonight. I appreciate the feedback and much thanks to all for taking the time to explain!




    Eating the elephant one byte at a time. -- author not known

  13. #13
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    One more thing... Make sure the directories in the described filename exist. Does "C:/windows/desktop/bt/" exist? If not, creating a file there won't work, and you'll get an error message.
    Insert obnoxious but pithy remark here

  14. #14
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by metaTron
    Greetings all,

    Any idea on the following code? This code is out of a beginners 'C' book; at first I got the error message, then I fixed the file location address and the error message went away. Now when I check the file for content, there is nothing.

    Note: I'm using Borland Turbo C++ Ver. 3.0

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    FILE *fptr;
    
    main()
    {
       int age = 22;	/* Simple variables to write */
    
       float salary = 19670.50;
    
       fptr = fopen("C:\WINDOWS\DESKTOP\BT\MYDATA.TXT", "w");  /* Opens for output */
    
       if (fptr == 0)
    	{ printf("An error occurred while opening the file.\n");
    	  exit (1);
    	}
    
       fprintf(fptr, "Here is some stuff:\n");
    
       fprintf(fptr, "I am %d years old.\n", age);
    
       fprintf(fptr, "I make $%.2f dolLars every three months!\n", salary);
    
       fclose(fptr);	/* Always close your files */
    
    return 0;
    }

    Eating the elephant one byte at a time.

    Couple of quick comments:

    It's always a good idea to be specific about main.

    As written, your main does not specify what it returns. Under C89 I believe any non-specified return values defaulted to int. Under C99 that is no longer the case. (However, I believe I read somewhere that the Borland compiler doesn't support C99 very well yet ). Still, it is good to get into the habit.

    Also, there's no real need to make the FILE pointer a global variable. It can easily be declared inside main, given the way you are using it....

    fwiw......

  15. #15
    Registered User
    Join Date
    Jan 2006
    Location
    Maryland
    Posts
    13
    Thanks for all the input on this, the easy fix was to correct the file address and now everything works as advertised.

    This is what I had--

    Code:
    fptr = fopen("C:\WINDOWS\DESKTOP\BT\MYDATA.TXT", "w");  /* Opens for output */
    This was the fix--

    Code:
    fptr = fopen("MYDATA.TXT", "w");  /* Opens for output */
    all comments noted and appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Print a File on Multiple Processors
    By Cell in forum Linux Programming
    Replies: 6
    Last Post: 03-25-2009, 09:39 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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