Newbie problem, fopen
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Newbie problem, fopen

  1. #1
    Join Date
    Oct 2011
    Posts
    91

    Newbie problem, fopen

    Hello,

    I am hoping someone can help with a problem I am having. I need to open a file to write an int and a float to it. So, I have been trying to build up my understanding to do this, so I figured I would try and get a small prohram working first. But that plan isn't going so well.

    The program is
    Code:
    /* fopen example */
    #include <stdio.h>
    int main ()
    {
      FILE * pFile;
      pFile = fopen ("myfile.txt","w");
      if (pFile!=NULL)
      {
        fputs ("fopen example",pFile);
        fclose (pFile);
      }
      return 0;
    }
    Now, can I ask, do I need to create an empty file called myfile? Or shoukd the program do that? If I do need to make the file, how does the program know where to find it??

    I have been reading the documentation, but I don't really find answers to these questions, they all assume a specific amount of understand to begin with it seems.

    Any help or advice would be fantastic! I want to learn how to do this, I don't want anyone to do it for me, but I do need to have some things explained.

    Also, is there an error in this program??

    I hope that's cool.

    Thanks in advance.

    Seán

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Newbie problem, fopen

    Quote Originally Posted by o.fithcheallaigh View Post
    Now, can I ask, do I need to create an empty file called myfile? Or shoukd the program do that?
    Since you opened the file for writing (with "w"), a file will be created if it doesn't already exist. The "w" flag will also cause any existing file to be completely overwritten. If you had opened it with "a" instead (append), a file would be created if necessary but if it already existed, your additional output would just be added, rather than replacing what was there before.

    If you had opened the file for reading with "r", the file would not be created if it did not already exist. The fopen would simply fail.

    If I do need to make the file, how does the program know where to find it??
    It uses the current working directory. If you are running your program from the command line, that is whatever location you are in before launching the program. With an IDE, it is usually the project directory or something close to it. You can change the CWD if you want via project options or API calls.

    Also, is there an error in this program??
    Looks okay at first glance. Does it work? (I don't recall if FILE* stuff requires stdlib.h or not. It might.)

  3. #3
    Join Date
    Oct 2011
    Posts
    91

    Re: Newbie problem, fopen

    Hey,

    Thanks very much for the reply, and the information.

    Actually, it did work. However, I didn't know it did ...I complied the program a few times, but no file was created in the folder ....then, I clicked on a .exe file that was the project and that created the file.

    Again, thanks a lot for your help!

    Seán

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Newbie problem, fopen

    Well yes, compiling the program isn't the same thing as running it.

  5. #5
    Join Date
    Oct 2011
    Posts
    91

    Re: Newbie problem, fopen

    Haha yea ...every day is a school day!

    Seán

  6. #6
    Join Date
    Oct 2011
    Posts
    91

    Re: Newbie problem, fopen

    If I can ask another question...

    I have been putting the full program together, to read in an int and a float, and then write them to a file. But this line isn't liked.
    Code:
    fputs("Your integer was: %06d and your float was: %5.2f",x,y,pFile);
    The error says there are two many arguments. Is there an alternative that will allow me to do what I need.

    The full peogram is below
    Code:
    #include <stdio.h>
    
    int x;           //Variables declared
    float y; 
    
    int main ()
    {
         
      printf("Please enter and integer and a floating point number\n");
      scanf("%d", &x);     //Scanning for input, saving to x
      scanf("%f", &y);     //saving to y
      
      //Below will display the information in required format     
      /*printf("Your integer was: %06d and your float was: %5.2f", x, y);
      scanf("%d", x);         //Used to keep the consol window open*/
      
       FILE * pFile;
      pFile = fopen ("myfile.txt","w");
      if (pFile!=NULL)
      {
        fputs("Your integer was: %06d and your float was: %5.2f",x,y,pFile);
        fclose (pFile);
      }
      return 0;
    }

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Newbie problem, fopen

    Seems like you want fprintf rather than fputs.

  8. #8
    Join Date
    Oct 2011
    Posts
    91

    Re: Newbie problem, fopen

    Still didn't like that. Here is the error

    23 C:\Users\Seán\Documents\Cpp\1\main.cpp cannot convert `const char*' to `FILE*' for argument `1' to `int fprintf(FILE*, const char*, ...)'
    Now, I am not sure what that means.

    Seán

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Newbie problem, fopen

    Read the printf documentation more carefully. You aren't passing the arguments in the correct order.

  10. #10
    Join Date
    Oct 2011
    Posts
    91

    Re: Newbie problem, fopen

    Ahh, got it!

    Go raibh míle maith agat mo chara!

    (A thousand thank yous my friend!)

    Seán

  11. #11
    Join Date
    Oct 2011
    Posts
    91

    Re: Newbie problem, fopen

    Hello,

    Thought I would bring this one back since it is a related question.

    I am now trying to open a file which I have created in the working folder. But it won't open. Can someone see what I am doing wrong?

    Code:
    /* fopen example, exercise 4 */
    #include <stdio.h>
    int n;
    int main ()
    {
      FILE *pFile;
      pFile = fopen ("open","r");
      if (pFile!=NULL)
      {
        printf("Didn't open",pFile);  //Display if don't open
        fclose (pFile);
        scanf("%d%",n);  //Used to keep concol open
      }
      return 0;
    }
    Thanks in advance.

    Seán

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Newbie problem, fopen

    You have a file called "open" in the working folder?

  13. #13
    Join Date
    Oct 2011
    Posts
    91

    Re: Newbie problem, fopen

    Hello,

    Yes, I do ...I tried calling it 'open' and open.txt', but nothing doing.

    Seán

  14. #14
    Join Date
    Oct 2011
    Posts
    59

    Re: Newbie problem, fopen

    Are you using an IDE (eclipse, MSVS, etc...)? I have a feeling that the cwd (current working directory) for debugging is not pointing at the same directory as where your file "open" is.

    To verify, at a command console, in the same directory as the executable, have your "open" file, then execute the programme. Does this work?


    A

  15. #15
    Join Date
    Oct 2011
    Posts
    91

    Re: Newbie problem, fopen

    Hello,

    Yes, I am using an IDE ...from Bloodshed.

    I'm sorry, I am not sure what you want me to do ...but I have everything in the one folder, my executable file, the file I want to open etc.

    And it worked fine when I was creating a folder.

    Se&#225;n
    Last edited by o.fithcheallaigh; October 28th, 2011 at 02:18 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center