advice about infile/outfile

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
Last edited on
> 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>

    const char* 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();

your want to first include <fstream>

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;
} 
Last edited on
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.

thanks thats a good point

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
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
using namespace std;
const int size = 10;
int main()
{
	string line;
	string name;
	string NAME;
	string LINE;
	string line2;
	string line3;
	size_t found;
	char file[size];
	ifstream infile;

	cout << " enter file name :";
	cin.getline(file,size);
	infile.open(file);
if(!infile.is_open())
{
	cout << " could not open file" << file << endl;
	cout << " program Terminating....\n";
	exit(EXIT_FAILURE);
}

cout << " enter string to search for ";
cin >> name;
name = (uppercase (NAME));


if(infile)
{
	while ( getline ( infile , line ))
	{
		LINE = ( uppercase(  line));
			LINE.find(NAME);
		if (found!=string::npos)
		{
			cout << line;
			(getline (infile, line2));
			cout << "\n" << line2;
			(getline (infile, line3));
			cout << "\n" << line3;
		}
	}
}



infile.close();

	return 0;
}


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


Last edited on
where you declare your ifstream infile
change infile.open(file); to infile.open(file.c_str());

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
this is just a theory of mine...

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
while(getline(file,line))
	{
	count++;
		
	                if(search == line)
		{
int line1 = count+1;
int line2 = count+2;
int line3 = count+3;
int line4 = count+4;
int line5 = count+5;
			if(count = line1)
			{
			getline(file,line);
			cout<<"line 1 is .. "<<line<<endl<<endl;
				
			}
			if(count = line2)
			{
			getline(file,line);
			cout<<" line 2 is .. "<<line<<endl<<endl;
				
			}
			if(count = line3)
			{
			getline(file,line);
			cout<<"line 3 is .. "<<line<<endl<<endl;
				
			}
			if(count = line4)
			{
			getline(file,line);
			cout<<" line 4 is .. "<<line<<endl<<endl;
				
			}
			if(count = line5)
			{
			getline(file,line);
			cout<<" line 5 is .. "<<line<<endl<<endl;
				
			}

                                             



			
		}

	}
Topic archived. No new replies allowed.