I am new to c++ and i have a couple of problems i have been tring to find a solution to but am coming up short!
first- is my infile am not sure what to use to read through the file find the string im looking for and displaying that string and the other 2 strings till my '#'
that i have inserted
second- i have been able to append to my oufile but how do i get the file to
write more than one string to a line
example of the file format
str1
str2
str3#
str4
str5
str6#
for anyone with advice-
I would rather you not post the code please! but if you could just point me in the right direction the reason is I dont think i will learn anything if i just copy and paste
> I dont think i will learn anything if i just copy and paste
Don't just copy and paste then. Study the code carefully. Play around with it. Remember that you do in fact need to look at quite a few example programs to learn about program structure and style.
That said, the common idiom to read a text file line by line is:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
constchar* const FileName = "data.txt";
std::ifstream fin( FileName );
if( !fin ) {
std::cerr << "Can't open file " << FileName << std::endl;
std::exit( -1 );
}
std::string line;
while( std::getline( fin, line )) {
// here you can check if line contains the string
// you're looking for or a string with a # at the end
// or whatever
}
fin.close();
the functions are called ifstream & ofstream (input/output)
to output to a file you simply just declare the output file by:
1 2
ofstream myFile("myFile.txt"); // Opens the file/ or creates if doesnt exist
myFile.Close() // closes the file
input file
1 2 3 4 5 6 7 8 9 10 11 12
ifstream myFile("myFile.txt"); // opens file
if(myFile.isOpen()) // checks to see if file was opened succesfully
{
string line = "";
getline(myFile,line,'#'); // display contents of file line by line
cout<<line;
myFile.Close();
}
else
{
cout<<"Error Reading File;
}
Hello Hammurabi
> Study the code carefully. Play around with it. Remember that you do in fact need to look at quite a few example programs to learn about program structure and style.
so that is what i have but when i run it i get these errors
cpp(32) : error C2664: 'std::uppercase' : cannot convert parameter 1 from 'std::string' to 'std::ios_base &'
cpp(39) : error C2664: 'std::uppercase' : cannot convert parameter 1 from 'std::string' to 'std::ios_base &'
but if i take out the uppercase and change LINE.find(NAME) to line.find(name)
I get
run time error the var. 'found' being used without being initialized
and it displays the whole .txt
infile.open(file.c_str());
When i use this it will not allow because file is an array
1 2 3 4 5 6 7 8 9 10 11 12
if(infile)
{
while ( getline ( infile , line ))
{
line.find(name);
if (line == name)
{
cout << line;
}
}
}
If I use this i can get it to find and display one string (the one im looking for) but i need it to display the 5 strings that follow the one i searched for and the one i searched for