Hello Beginner of C,
The following program illustrates a different, I feel a better, way to deal with your problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include<iostream>
#include<iomanip>
#include <chrono>
#include <thread>
#include<fstream>
#include <Windows.h> // <--- For "GetLastError()" function.
//using namespace std; // <--- Best not to use.
int main()
{
const std::string PATH{ "C:\\users\\Desktop\\Jaswinder Singh\\" };
const std::string outFileName{ "myfile.txt" };
std::ofstream outFile(PATH + outFileName);
if (!outFile)
{
std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
std::cout << "\n Error number is: " << GetLastError() << ' ';
if (GetLastError() == 3) std::cout << std::quoted("ERROR PATH NOT FOUND") << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5)); // <--- Needs header files chrono" and "thread". Optional as is the header files.
return 1; //exit(1); // If not in "main".
}
//myfile.open("C:\\users\\Desktop\\Jaswinder Singh\\myfile.txt"); // <--- Better, but missing a subdirectory.
//myfile.open("C:/users/Desktop/Jaswinder Singh/myfile.txt"); // <--- Will work, but still missing a subdirectory.
outFile << "Writing this to a file\n";
outFile.close();
return 0;
}
|
To start with lines 2 and 3 like line 27 are optional. In my set up for VS when a return or exit function is executed the window, the program is running in, closes. This allows me to see the message before it is gone. Although optional, if the program was compiled for release line 27 may be useful.
The only part of line 27 you need to understand is:
seconds(5)
. as the name implies the "5" is whole seconds. You could use "milliseconds" where (1000) is equal to one second and say (500) is equal to 1/2 second and (1500) is 1 and 1/2 seconds. It gives more control over the wait time. This is one of the nicer parts about C++. You do not always have to know how something works just how to use it.
Lines 23 and 25 I added for this example to help show what the problem is. Normally I do not put these lines in the if block.
Lines 13 - 30 is an overall concept. I tried to make its use as generic as I could and that is why I put the file name in a variable with line 15. Most of the time I leave out lines 23 and 25. /when learning about files I found this to be useful to understand what is happening.
The similar concept:
1 2 3 4 5 6 7 8 9 10
|
const std::string inFileName{ "" }; // <--- File name goes here.
std::ifstream inFile(inFileName);
if (!inFile)
{
std::cout << "\n File " << std::quoted(inFileName) << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread". Optional as is the header files.
return 1; //exit(1); // If not in "main".
}
|
For an input file this is a must have. Not so much for an output file, but it is helpful as it is with your problem.
For an output file is the file name does not exist it will create the file when the file stream is opened. Unlike the input file which has to exist first.
Line 17 and line 3 in the second bit of code does two things. First it creates the file stream and then opens the file in one line of code.
Note: The path here is purposely incorrect to illustrate the use of the if statement. Lines 32 and 34 I think the comments explain it.
Anything you do not understand let me know.
Hope this helps,
Andy