Fstream help

Hi, I'm currently having trouble loading a file from a folder. I have 1 folder containing the code and inside that folder containing the file.txt. I have been trying to get the Ifile to go into the folder and read the data. but I have no luck. I really don't want to do full path (nor do I know how to do full path). for I plan on sending the code to friends and my dad to check out. sorry if this is a really easy question or a tough one...
-Reikon
edit:

The file is located in my documents. the second is located in folder testing
I intend to give my friends the file via .zip so they can unpack it and run the applications
my executable is located in the same folder testing.

In short, is there a way to shorten the file path from

"C:\\user\\Documents\\Testing\\file\\filetest.txt"
to
"C:\\Testing\\file\\filetest.txt"
(File is the folder containg the file)
(Testing has the excutable)
sorry for the confusion.

this is the test code I made
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
  #include <iostream>
#include <fstream>
using namespace std;
 int main() {
 	
 	 //var
 	 ifstream Ifile;
  	 ofstream Ofile;
  	 string A,B,C,D,E;
  	 char a;
 	
 	 // code
 	cout << "Whould you like to try to open the file?" << endl;
 	cin >> a;
 	if (a=='n')
 	return 0;
 	else 
 	cout <<"trying to open" <<endl;
 	Ifile.open("C:Testing\\file\\filetest.txt");
 	if (Ifile.is_open()) {
 		Ifile>>A>>B>>C;
 		cout << A << " " << B <<" " << C<< endl;
	 }
	 else {
	 	cout << "It did not work :(" << endl;
	 }
	 }
 
 
Last edited on
I have been trying to get the Ifile to go into the folder and read the data. but I have no luck. I really don't want to do full path (nor do I know how to do full path).

Where exactly is your file located?

Where exactly is your executable located?

Why are you specifying the C: drive and a "partial" path?

How do you intend your "friends" to run the program?

Normally for a file located in the same directory as the executable you would use something like: ""filetest.txt".


Hello Reikon,

In addition to jlb's questions it would help to know what IDE you are using and on what operating system. This can make a difference when it comes to knowing what is considered the "current working directory" when running or debugging the file.

When I tried to test your program the first compile was full of errors. On line 9 you define a "std::string" which is fine, but you need to include the header file "<string>" to be able to use this.

Try to avoid single letter variable names. This may seem easy and not a big deal in a small program like yours, but when your programs get larger you and someone else trying to help you can loose track of what variable to use. It is best to a noun that describes what the variable is or does. Instead of "A", "B", "C", "D" and "E" the names "srting1" or a shorter version "str1" etc. are much easier to follow. And for the "char" "choice" is more often used for the way you are using it.

As a tip: it is generally accepted that regular variable names start with a lower case letter and use "camelCase", (where the second and each successive word starts with a capital letter), you could also use the old DOS underscore to show a space between words like "camel_case". Next "class"s and "struct"s start with a capital letter and you can still use the camelCase if there is more than one word. When it comes to variables defined as constants with the key words "const" or "constexpr" these are all capital letters. I find that the capital letters help to remind you that this is a variable that can not be modified by the program when running. An example: constexpr size_t MAXSIZE{ 10 }; and you can use it with int nums[MAXSIZE]{};. The empty {}s will initialize the array to all (0) zeros.

"size_t" is something you should get use to seeing and using. "size_t" and "size_type" are both aliases or, (another name for), an "unsigned int" which is the return value for many class member functions like "string.size()" or "string.length()". Both return the same number that being the amount of elements in the string.

When I have finished it I will show you what you can do with your program.

For now knowing the IDE and operating system will help. Also since you are using an input file for the program post the file content or a fair sample if it is large so everyone will know what the file looks like and will be using the same information. This also helps to better understand if you are reading the file properly or if you have a problem.

Hope that helps,

Andy
I'm using dev c++
if that helps.
And I know all about string...I just forgot it, hahaha.
Thanks for all the tips Handy Andy, I just used them to test how to shorten the path.
Last edited on

In short, is there a way to shorten the file path from

"C:\\user\\Documents\\Testing\\file\\filetest.txt"
to
"C:\\Testing\\file\\filetest.txt"
(File is the folder containg the file)
(Testing has the excutable)
sorry for the confusion.


Do you know the difference between an "absolute path" and a "relative path"?

Do you know what Andy means by "working directory"?

Do you know that "C:\user\Documents\Testing\file\filetest.txt" and "C:\Testing\file\filetest.txt" are two different files located in two separate directories on your C: drive? Do you realize that "C:Testing\file\filetest.txt" may or may not be the same file as "C:\user\Documents\Testing\file\filetest.txt", depending on where the current C: drive's working directory is pointing?

If you are unsure on any of the above you need to find and read some documentation for those topics for your operating system.

Please don't edit a post after there has been another post, it makes following the topic almost impossible. In this case you modified your examples that affected several of the questions in the following posts.

I apologize Jlb, I'm very new to the these help message boards, I really don't know much about files my self. I will try to read something on files and how they work. I was only taught about how to read and write a file. not the directory and stuff. sorry for being such a dunce. I just thought is there a way I can shorten the file pathname, Because the user is different for all computers, so I want it so they can access the files in the same area, with having to change users and where else they change the file location to. I made that test program very quick, to test out my self so it does not have my best efforts put in. Again sorry for my lack of knowlage I was just introduced to c++ in September.
The easiest way to "shorten" the file name is to put your text file in the same directory as the executable. Then in the program you only need to specify the file name, not the path.

But the question will still be how are your "friends" going to execute your program? Do they know how to traverse their file system to be able to find the directory where the executable resides?

Edit:
Again sorry for my lack of knowlage I was just introduced to c++ in September.


This is not really a programming problem, it is a file system problem. Until you know how the file system works it will be hard to "locate" the text file inside of any program.

Last edited on
Okay, Thank you very much, Jlb. and for the friends part. I was going to send the .exe to them in a zip with the code in all. I may have to show them personally.
Last edited on
Topic archived. No new replies allowed.