Need Help with *Understanding Concepts* with fstream

So, I recently started using name "fstream" in my programs. I even have an assignment using fstreams for class, which is really...really basic, so before I start it, I wanted to check my understanding first.

I have done a little re search on my own to understand some basics.
what I have below is the code, and what my understands of how it works.

If anyone has any suggestions/corrections, etc that would be awesome, Thank you.

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
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>

using namespace std;

int main() 

{

ofstream myfile;   // this is where I nameed my "varible for when I send out to a file

ifstream The_Input; // "The_Input" is my varible for when I take date from a file
                    // The way I thought of it is as a replacement for "cin" that 
                    // that can work for a file, as long as I name the file. (an 
                    // an example of a name is below.

string Line, Line2; // These are just my strings ( I understand these ^^)
int b;              // My integers I understand these as well ^_^


myfile.open ("example.txt"); // This is where I first chose my output folder
                             // and open/create the new txt file
                             
myfile << "Writing this to a file, and I rock if this works.\n";// this is the 
// The output for the file "example.txt" a replace for "cout". 



//myfile <<endl ;***** I notice when I used "myfile <<endl;" to add a blank line
// it worked in the sense that I got a black line, but when I tried to inpute
//from this file, it gave me issues, I found it better just to use "\n\n\n" for 
//new lines

myfile << "\tThis is line two, tell me if you print out.\n"; // this was just a
//second line for me to test the program


// My goal was to have the first out get assigned to "Line" and the second ouput
// to get assigned to "Line2"

myfile << 7 +8 ;  // I just threw that in to test inputing/withdrawing integers

myfile.close(); // I closed the "example.txt file here

The_Input.open ("example.txt"); // Here Im reopening the txt file so that I can
// assign the day within to my declared strings and int's

getline(The_Input, Line);  // from trail in error I realized I had to use "getline"
// because "The_Input >> line" only gave me the first word of my txt file, up to my
// my first space
// ******The issue here is, is there a way to control "what" I input from this line?
// I found a tool called ".ignore"  but Im not sure if that will help me, or even how
// to properly use that yet.  :-(

//The_Input.ignore (100);     // My failed attempt at using ignore "nothing came up"
getline (The_Input, Line2); // here (if Im correct), is where the program goes in 
// the txt file, grabs the "Second line" and assigns it to "Line2"

The_Input >> b; // (If Im correct) this is where my programs goes into my tx and 
// and grabs my FIRST integer number in the txt.

 cout << Line <<endl;  // This is just me testing what was I assigned
cout << Line2<<endl;  // Same as above
cout << b <<endl;  // testing interger  theory out lol
The_Input.close(); // closeing the example.txt file again/stop the program from
//inputing data from there

system ("pause");
return 0;
// The End IF anyone could tell me if Im on the right track or not, that would be 
//greatly appericated, any tips I would love. 

// And any explantion of how the input works with integers and strings, does it really
// look for the first integer in txt? or does it go in the order?
// Thanks guys I just really wany to undestand better, and like I said before
//any input would be appericated
}
Last edited on
Topic archived. No new replies allowed.