Thread: Help with Do While loop

  1. #16
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Quote Originally Posted by samwillc View Post
    Code:
    ofstream a_file("test.txt",ios::app);
    I have a few questions about this if you (or anyone else) wouldn't mind indulging me:

    1) Where on your computer is the file "test.txt"? Could this be written as a path? i.e. "/Users/Sam/Documents/test.txt"
    2) If this file does not exist, is it created?
    3) If it does exist, is the question appended to the end of the text in the file or is the file re-created? i.e. do earlier questions persist within the file or not

    Thanks for any extra info.
    To answer your questions in order:

    1. The file will be located in the same folder as your code file.
    2. Yes.
    3. That depends on how your write the command. If you put ios::app at the end, it will append to the file. Otherwise it will erase whats in the file.

    To learn more about file I/O, go here:

    C++ File I/O Tutorial - Cprogramming.com

    I'm basically going by this tutorial and I'm up to this part now. So figured I would try this with a loop to see if I could create a simple program to write to files.

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    1. The file will be located in the same folder as your code file.
    Actually it will be located in the current working directory, not necessarily where your code is located. If you are using an IDE to compile and run the program the current directory is usually where you project files are located, or where the project places the executable, depending on your IDE settings. If you are running the program from the command line, the working directory is the location where you ran the program.

    Could this be written as a path? i.e. "/Users/Sam/Documents/test.txt"
    Yes you can use an absolute path instead of the relative path you get when you use just the filename.

    Jim

  3. #18
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Thanks everyone for the info. I'll bear this in mind when I get to that bit in the book!

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by samwillc View Post
    Code:
    ofstream a_file("test.txt",ios::app);
    I have a few questions about this if you (or anyone else) wouldn't mind indulging me:

    1) Where on your computer is the file "test.txt"? Could this be written as a path? i.e. "/Users/Sam/Documents/test.txt"
    Probably not the same place. "test.txt" refers to the current working directory, which is probably the project directory if you're using an IDE. When you run the linked executable, it is possible that the current working directory is set to something else, which you can change. You can also change the current working directory outside of the IDE: Say for example you temporarily set PATH to include your executable, then you can run the program like this.

    C:\Users\Josh2>cd documents

    C:\Users\Josh2\Documents>cd itcs2590

    C:\Users\Josh2\Documents\ITCS2590>program

    2) If this file does not exist, is it created?
    3) If it does exist, is the question appended to the end of the text in the file or is the file re-created? i.e. do earlier questions persist within the file or not
    C++ File I/O Tutorial - Cprogramming.com
    ofstream - C++ Reference

    If the file does not exist and is not created, a zombie file stream is created that can't really do anything. File streams in C++ do not throw exceptions for this error by default. It is therefore important to check the status of the stream before you start trying to write (in this case) to the file.

  5. #20
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Nice info, thanks.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe that opening a file for appending actually fails if the file does not exist. It never creates it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I believe that opening a file for appending actually fails if the file does not exist. It never creates it.
    Did you try?

    When using an ofstream the file will be created when it doesn't exist by using the following second argument: noargs, std::ios::ate, std::ios::app, std::ios::trunc, std::ios:ut, std::ios::in or any of the combinations of the above.

    When using an ifstream the file will be created when it doesn't exist by using only the following second argument: std::ios::app.

    When using an fstream the file will be created when it doesn't exist by using the following second arguments: std::ios::app (with or without being OR'd with std::ios::in or std::ios:ut, or both), and std::ios:ut (but not std::ios::in || std::ios:ut, except when OR'd with std::ios::app).

    As you can see using an ofstream will create the file if it doesn't exist, no matter what arguments I used. So whenever you have problems with files not opening properly the first thing I recommend is you try to create an ofstream. If it doesn't succeed in opening a file then there is usually something wrong with your access permissions or something else, perhaps the file is already opened in exclusive mode somewhere else. This also makes finding the current working directory easier when used with a distinct file name, since if it creates the file you can use your operating system's find functionality to locate the file.

    Both the ifstream, and fstream are much more restricted when it comes to creating a non-existent file.

    Jim

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, I didn't. Hence, I used the word "believe". I checked cplusplus.com, but it mentioned nothing about what happens with different parameters passed in. Hence, I raised some concerns.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Finding how the different flags are used is not too easy, but here is a table that correlates the ios_base::open_mode flags to the c-stdio functions.

    Help with Do While loop-table132-png

    This table is from the last C++11 draft Section 27.9.1.4 Table 132.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop stops working when loop >45 ??
    By Zul56 in forum C Programming
    Replies: 3
    Last Post: 12-11-2012, 03:40 PM
  2. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  3. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM