reservation

Can somehow teach me the basic of fstream, and also how to make a reservation using that

I plan to make a reservation that the list can be save into a txt file.

Reservation Operation:

- add and remove reservation

I need a tip about how to start with it
std::ifstream input("filename");
creates an stream for input, use it like `cin'

std::ifstream output("filename");
creates an stream for output, use it like `cout'

¿do you care about binary mode?


> I plan to make a reservation that the list can be save into a txt file.
¿what? ¿what list? ¿what's a reservation?
Hello CoolAvocado,

I believe ne555 in the second example meant std::ofstream output("filename");. Easy typo.

This may help http://www.cplusplus.com/doc/tutorial/files/

When I started working with files I started with something like this. This code has been refined over time to what it is now.

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
#include <iostream>
#include <iomanip>  // <--- For "std::quoted". "std::setprecision", "std::setw" and others.
#include <string>

#include <chrono>
#include <thread>

int main()
{
	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(5));  // <--- Needs header files chrono" and "thread". This line is optional as is the header files.
		return 1;  //exit(1);  // If not in "main".
	}

	const std::string outFileName{ "" }; // <--- File name goes here.

	std::ofstream outFile(outFileName); // <--- Use ", std::ios::app" after the variable to reopen the file for append.

	if (!outFile)
	{
		std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(5));  // <--- Needs header files chrono" and "thread". This line is optional as is the header files.
		return 1;  //exit(1);  // If not in "main".
	}

	 // <--- Rest of code here.

	return 0;
}

The if statements check if the file opened properly. This is a must for input files. Not as necessary for output files, but worth using.

The only thing I might add this is const std::string PATH{ "" }; // <--- the path goes in the quotes. at the beginning of main so both pieces can use it.

This should give you a start. After that, if you have any problems, post your code and we can work fro there.

Hope that helps,

Andy
Andy thankyou , does std::string filename is the same using #include <fstream>

My teacher teach us about #include <fstream>

what does #include <iomanip> do?



I am a newbie sorry for too many question ,
Thank you Everyone
Reservation is like

for example I am a dentist , so instead that my customers going to wait at my clinic , waiting for hours to get treated . SO if i have reservation they can just call me and RESERVE them let say other day with a time .. Then thats were the system works I ADD a client if they call me, Then I REMOVE client if they Cancel or Done.


THIS CODE ERROR i dont know how to fix buttttt i think its easy to fix i just dont know how

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct reserve
{
   string name;
   int time;
};
int main()
{
 reserve reservation;
 fstream myFile;

 myFile.open("reserve.txt",ios::in|ios::out|ios::app);
 for (int i=0;i<2;i++)
 {
     getline(cin,reservation.name);
     getline(cin,reservation.time);

     myFile<<reservation.name<<"\n"<<reservation.time<<"\n";
 }



 return 0;
}
Hello CoolAvocado,

does std::string filename is the same using #include <fstream>


No. "std::string" is from the "<string>" header file and not to be confused with "<string.h>" which is a C header file for working with strings.

"<fstream>" is a header file for working with files.

"<iomanip>" is a header for working with input and output streams. Mainly "cout", "cin" along with an input or output file streams. "<iomanip>" allows you to use functions that manipulate the output and input. Mostly used for output. As I used std::quoted(outFileName this takes what is in the () and puts double quotes around it, as I used it, or it could be a quoted string, using double quotes, that is two or more characters. Do not worry too much just use what I showed you.

What you are most likely to use first from "<iomanip>" is:
std::cout << std::fixed << std::showpoint << std::setprecision(2);. Where "fixed" says to use numbers not (scientific notation). The "showpoint" tells the output stream to print ".00" if it would show up. And "setprecision" says how many digits to the right of the decimal point to print. This is not needed for this program, but the code is worth keeping for the future.

For now I will just say that "setw()" is a way to line up the output to the screen or file.

As the comment says in the above code this line std::this_thread::sleep_for(std::chrono::seconds(5)); is optional as is the header files "<chrono>" and "<thread>". You do not have to understand everything about these header files or the line of code. The only part you need to deal with is if it says "seconds" the number in the () is the whole number os seconds this will wait before continuing with the program. The other option is "milliseconds" where (1000) is equal to one second.

Hope that helps,

Andy
andy can you check whats the problem on the code i send?
Hello CoolAvocado,

Yes. I think you posted your code while I was working on my last ersponce and I missed it until this morning.

I will load it up and give it a test.

The only part I do not understand right now is the for loop. It is OK for testing, but not something I would consider doing in a working program.

Andy
Hello CoolAvocado,

My bad. When I saw the line to open the file followed by the for loop it struck me as it was reading from the file. Later I realized it was reading from "std::cin".

"std::getline()" only works when the variable is defined as a "std::string". The second "getline" does not work because "time" is defined as an "int".

What I would add to the for loop is: std::cout << "Enter your full name: ";. Notice that there is no "\n" or "std::endl" and that it ends with ": ". This allows the "cin" to be on the same line as the prompt. It could just be me, but I think it works better this way.

Following the prompt for the time the code would work better as:
1
2
3
4
std::cout << "Enter the time (1122): ";
std::cin >> reservation.time;

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Added. Requires header file <limits>. 

In the prompt you need to give an example of how you want the time entered or you may get something different.

The last line clears the input buffer before you use "getline" again. This needs to be done or "getline" will extract the new line from the input buffer and continue not letting you enter anything.

All of this works. It does create the file if it does not exist and then writes to the file. Even if you run the program several times it always adds to the file.

When I tested reading the file I had to reset the file pointer back to the beginning/ This will need some care to know where the file pointer is when reading and writing to the file. especially if reading the file sets the "eof" bit.

Hope that helps,

Andy
IT helps Thank YOu ahahhahaha

:D :D
Andy,

I just want to know why does doesnt work but it run
1
2
cout<<strOutput;


there is no error while i hit the run button butt the code above doesnt read? dunno

here is the complete code for now

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
struct Teacher
{
    string name;
    string date;
};
int main()
{
    Teacher myTeacher;
    fstream myFile;

    myFile.open  ("sampleFile.txt",ios::in|ios::out|ios::app);

    for(int i=0;i<2;i++)
    {
        getline (cin,myTeacher.name);
        getline (cin,myTeacher.date);

        myFile<<myTeacher.name<<"\n"<<myTeacher.date<<"\n";

        }
string strOutput ;
myFile.seekg (0,ios::beg);

int lineCounter =1;
int numOflines= 2;

cout<<"NAME: \t\tSUBJECT:\n";

while(getline(myFile,strOutput))
{
    if (lineCounter==1)
        {
            myTeacher.name= strOutput;
            cout<<"\n"<<myTeacher.name;

        }
        else if(lineCounter==2)

        {
            myTeacher.date=strOutput;
            cout<<"\t\t"<<myTeacher.date;

        }
        if(lineCounter==numOflines)
        {
            lineCounter=0;
        }
        lineCounter++;
}



cout<<strOutput;



myFile.close();
cout<<endl;
return 0;

}
Hello CoolAvocado,

Line 59 is a new one for me. I would guess that when the while tries to read past end of file the variable "strOutput" is set to an empty string. Anyhow since it is an empty string you can not tell where it is printing nothing, but otherwise it does not cause any problems.

BTW "strOutput" is a bad name. Since you are reading the file as input "strInput" would be better.

The next part is the while loop. Why all the extra work? For what you are doing all you need is:
1
2
3
4
5
6
7
while (getline(myFile, strInput))
{
	cout << "\n " << strInput;

	std::getline(myFile, strInput);
	std::cout << "\t\t" << strInput;
}

The next problem I found is with the "\t"s. When the name becomes more than 6 characters the output looks like this:

 NAME:          SUBJECT:

 Name 1         1215
 Name 2         1317
 Name 3         1122
 NAME 4         1225
 Name 5         1122
 Name 6         1225
 Name 7         1430
 Name 8         1936
 Name 9         700
 Name 10                900
 Name 11                1422
 Name 12                2020


 Press Enter to continue :



When I revised the while loop this code:
1
2
3
4
5
6
7
while (getline(myFile, strInput))
{
	cout << "\n " << std::left << std::setw(15) << strInput;

	std::getline(myFile, strInput);
	std::cout << std::setw(4) << strInput;
}


Produced this output:

 NAME:          SUBJECT:

 Name 1         1215
 Name 2         1317
 Name 3         1122
 NAME 4         1225
 Name 5         1122
 Name 6         1225
 Name 7         1430
 Name 8         1936
 Name 9         700
 Name 10        900
 Name 11        1422
 Name 12        2020


 Press Enter to continue :



The variables "lineCounter" and "numOflines" are no longer needed.

The struct may have a use in the future, but for now it is not necessary because one "std::string" variable can do the work of the struct. For now you have proven that you know how to use the struct for output. What you have done in the for loop can be reversed in the while loop.

For now the program is simple and using a struct is more than you need. In the future putting the struct into a vector or array would have more use.

Hope that helps,

Andy
ANdy ANDYYYY ANDYYY HAHHAHAHAHHA
TNXXXXXXXXXXXX thats help alot


Honestly while i am reading your reply I see hope but there is also some things i dont understand . Im going to study the code and use theeee revised code you send about the output

may i ask a question?

i see that there a "Press Enter to continue " , Is that an exit or a system pause ?
sorry i am newb ahahaha

Im not familiar about using std too ...

Topic archived. No new replies allowed.