C Microsoft Visual Studio 2015 Fstream Read File C Microsoft Visual Studio 2015 Read File

Where To Add a Text File in Visual Studio 2017

I can not seem to effigy out how to add a text file to my project and then that the program can notice and open up a user specified file. I copied this code from my textbook but to endeavour and test where and how to load a text file for opening and reading data from. I have tried adding new item and calculation existing detail and when i run the program it notwithstanding cannot notice the file fifty-fifty though information technology is loaded into my projection using the above methods.

                          one
2
iii
iv
5
6
7
8
9
x
11
12
xiii
14
xv
16
17
eighteen
xix
twenty
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
                                                      #include <iostream>                            #include <string>                            #include <iomanip>                            #include <fstream>                            using                            namespace                            std;                            int                            main() {  	string input; 	fstream file;                            char                            ch;    	cout <<                            "Delight enter the file name. "; 	cin >> input;  	file.open up(input, ios::in);                            if                            (file) {  		file.become(ch);                            while                            (file) { 			cout << ch; 			file.go(ch); 		} 		file.close(); 	}                            else                            {  		cout <<                            "Error! "                            << endl; 	}   	system("pause");                            return                            0;  }                        

I have also tried calculation the txt file to the same location as my .cpp and rerun the program and nonetheless get Error displayed.

If you run a plan from VS the electric current directory is the same where the solution file (.sln) is.
If you run the programme from the command line the current directory is either in the Debug or Release folder.
Likewise on line 32 it's improve to use perror(nullptr); because it gives a more meaningful error msg.
You need to include <cstdio> for that.

Howdy maenielwolf,

I exercise not see VS 2022 beingness much different than VS 2022 that I use. When I started with VS I had a hard time with where to put a text file to employ with a program. Eventually I found this path to work well when the IDE is in "Debug" way. "C:\Users\Owner\Documents\Visual Studio 2015\Projects\Project proper name\Project Name". Where "Project Proper noun" is the name of the project you are working on.

If y'all compile the programme in "Release" manner the path volition modify because of where the ".exe" file is put. You can either adapt the path in the programme or copy the files to "...\Projects\Project Name\Release" sub directory.

Annotate out lines fifteen - 18 and add this code and run the program:

                          1
2
iii
4
5
6
7
8
9
10
11
12
13
fourteen
15
sixteen
17
xviii
nineteen
20
21
22
23
24
25
26
27
                                                      #include <iostream>                            #include <string>                            #include <iomanip>                            #include <fstream>                            #include <chrono>                                                        // <--- Added for lines 18 and 23.                            #include <thread>                                                        // <--- Added for lines 18 and 23.                            std::string iFileName{                            "Out File Test.txt"                            };                            // <--- Add File name here.                            std::ifstream inFile;  inFile.open(iFileName);                            if                            (inFile.is_open()) {                            //  Annotate out these lines when program is working the way you want.                            std::cout <<                            "\n File "                            << iFileName <<                            " is open"                            << std::endl; 	std::this_thread::sleep_for(std::chrono::seconds(2));                            // <--- Needs header files chrono" and "thread".                            }                            else                            { 	std::cout <<                            "\n File "                            << iFileName <<                            " did not open"                            << std::endl; 	std::this_thread::sleep_for(std::chrono::seconds(3));                            // <--- Needs header files chrono" and "thread"                            leave(one); }  exit(0);                        

When the plan has run search for "Out File Test.txt" and see where it is at. Put your input file in the same directory and all should work.

Experience costless to use this code when working with files. Slight changes are needed to use for output files. Remember to remove the "exit(0); at the end of the code. Information technology is not needed for normal use.

I have found that the ".txt" file need then exist in the aforementioned sub directory equally the "stdafx.h", "targerver.h" and "ReadMe.txt" files.

Hope that helps,

Andy

Thanks for all the help.

Andy I put your code in to test and see if the file was in the right location and establish that it needs to get in the same Project folder equally my .cpp file then I simply add existing file from that directory to the solution source files.

So I know i have the file in the right location at present but the original program still doesnt read and open the file. I dont sympathise what the result is as the program is directly from the text so there is no reason it shouldnt piece of work. This is why i didnt recall calculation the text file to the same location equally my .ccp file was working. Am I missing something? Why wont my text file open up and display an screen?

Last edited on

Also on line 32 it'southward better to use perror(nullptr); because it gives a more meaningful error msg. You lot need to include <cstdio> for that.

Didn't you lot endeavor?

Thomas1965,

Yes I did it says "no such file or directory". I am not sure why Andy's suggestion would be able find the file though. I accept the file saved in the the same location as the .cpp file. I tried calculation it to the same location as the .sln file and had no luck.

Hello maenielwolf,

Sorry for the wrong code. I meant to set upwardly the code for output to create a file, but started working with the wrong code.

One possibility, my bit of code put the file proper noun in the variable "iFileName" where as yous code takes input from the user. What might be happening there is that the file name entered might exist misspelled or the extension is missing.

I tried your code and entered a file name with the extension and it worked.

Also y'all do non need to add the input or output files to the project solution to use them.

I do not know what you are using equally a file proper name, but on line sixteen it would work better to use "std::getline(std::cin, input);" instead of the "std::cin >> input;". And so if your file proper name has a space in it the "std::cin" will read upward to the showtime infinite and end leaving the residual of the file name in the input buffer for any adjacent read.

Hope that helps,

Andy

I was able to get it to work. Similar the noob I am i wasnt typing in txt after the file name. Cheers again for all the help.

Topic archived. No new replies allowed.

mcdowellwhoubson.blogspot.com

Source: https://www.cplusplus.com/forum/beginner/217118

0 Response to "C Microsoft Visual Studio 2015 Fstream Read File C Microsoft Visual Studio 2015 Read File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel