Hey! I'm trying to store the first line of a NotePad document into a string. I already know how to "Open" this file so it can be read and written into, i know how to write INTO it, but I dont know how to get OUT of it. Any help? thanks!
If you havr any questions, just ask! Thanks a lot!
If you are using an fstream, you can operate on it the same was as you would with cin and cout respectively. So you could use either file>>string;
or getline(file, string);.
When using getline for file input, its often helpful to use the 3rd argument of getline, which is the delimiter.
You can then format your input file with non '\n' delimiters.
If you omit the 3rd argument, the delimiter defaults to '\n'.
Use the deliminator parameter to specify a character for the getline function to stop at. Then, format the file however you need to get the info you want.
file.txt
1 2 3
name=bob
job=programmer
end=
reader.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
ifstream file(file.txt);
string line, name, job;
//loop to read file
while (true){
getline(file, line, '=');
if (line=="name")
getline(file, name);
if (line=="job")
getline(file, job);
if (line=="end")
break;}
cout << name << endl;
cout << "Job: " << job << endl;
What does ifstream file do?
I hate doing things without knowing what they mean.
I am at this part in my course and nothing is explained it just says DO THIS.
Im writing words with no idea why, other than because it makes the program not broke.
The only snippet of code i was given in my programming course for data input from file was this:
Creates a new variable that holds information about the file, where the current read buffer is, write buffer as well, and other things. It allows you to access the information found in the file.
use the "file" word just like you would cin. So if you want data, you can use >> or if you want a single character you can use .get();
1 2 3 4 5 6 7 8 9 10
fstream file;
file.open("wheremyfileat\file.ext",ios::in);
//ios::in is a flag, it tells the program we want information, change to ios::out
//to send data to the file.
while (!file.eof()){ //file.eof() checks to see if we've gotten the end of file character from the file.
cout << file.get(); //this will output every single character in the file, one at a time.
}
file.close(); //when we close the file, we give up rights to it, so now someone else can use it
Thankyou Ultifinitus that helps alot! I wish they just included that info in the course so we don't have to bug people who aren't getting paid to teach us -_-
Sounds good! But i need help getting started still D: This is my function in my assignment.
Supposed to get 1000 numbers 1000 times (one million values) for each number randomly chosen you keep track of how many of each so (if 1){array[1]++} etc f that makes sence.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void GetRandom(int *numbers) // Sets array of 1000 to random values
{
int i=0;
int ii=0;
int x=0;
for(i=0; i<999; i++)
{
x=rand()%1000;
for(ii=0; ii<999; ii++)
{
if(ii==x){numbers[x]++;}
}
}
}
I need to send the random number generation to a file, and then retrieve it from that file... I was probably supposed to make 2 seperate functions.. but im kind of a slacker.