getline Questions..Appreciated!



// Hello <p> blah blah<h1>bl blah
//using the line above i wrote this code(below) to attempt to gather just the
//<p>..i have no errors..but i also have nothing outputting to the screen?
//what is wrong with my syntax?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string x = " ";
	string temp = " ";

	while (!infile.eof()) {
		getline(infile, temp, '<');
		getline(infile, x, '>');
		x = x + '>';
		cout << x << endl;
		if (x == "<p>")
		{
			cout << "y" << endl;
		}
	}

Last edited on
There is no way of knowing on lines 7 through 12 if the extraction operations on lines 5 and 6 failed.

getline removes and discards the delimiter from the input stream.
could you tell me how i would stop right before the '<' to include it?
could you tell me how i would stop right before the '<' to include it?

Not really worth it..

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
#include <iostream>
#include <limits>
#include <sstream>
#include <string>

std::istringstream in(
R"(Hello <p> blah blah <h1> bl blah
 *** << Rutroh >> ***<
)"    
);

std::string bracketed(const std::string& s)
{
    return '<' + s + '>';
}

int main()
{
    const std::streamsize lim = std::numeric_limits<std::streamsize>::max();

    while (in.ignore(lim, '<'))
    {
        std::string extracted;
        if (getline(in, extracted, '>') && !in.eof())
            std::cout << "Found \"" << bracketed(extracted) << "\"\n";
    }
}
Found "<p>"
Found "<h1>"
Found "<< Rutroh >"
1
2
if (getline(in, extracted, '>') 
   && !in.eof()) //¿what for? 


ah, so it must parse a closing >
Last edited on
Thanks allot, i found a way.

Last edited on
1
2
3
4
5
std::find(
	std::istream_iterator<std::string>(input),
	std::istream_iterator<std::string>(),
	"foo"
)
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

//I am getting an error not sure what it means or why I am getting it
//Debug Assertion Failed!
//File:stuff.....\include\xstring
//Line:1175
//Expression: invalid null pointer
#include<iostream>
#include<fstream>
#include<string>
#include<cmath>

#define ifile "input1.txt"
#define ofile "out.txt"

using namespace std;
string scan(string,ifstream&);//prototype
//void arrayStow(string);//prototype


string scan(string temp, ifstream&infile) {//scans file for specific <P><BR><IMG...>
	string hold;
	
	
	getline(infile, temp, '<');
	getline(infile, hold, '>');
	
	if (hold == "P") {
		return '<' + hold + '>';
	}
	if (hold == "BR") {
		return '<' + hold + '>';
	}
	if (hold == "IMG SRC='http://image.jpeg'")
	{
		return '<' + hold + '>';
	}
	else
		return 0;

}
int main() {
	ifstream infile;
	ofstream outfile;
	string line = "", P = "";
	infile.open("input1.txt");
	outfile.open("out.txt");

	while (!infile.eof()) {
		P = scan(line, infile);
		//arrayStow(P);
		cout << P << endl;
	}

	system("pause");
	return 0;
}
Last edited on
scan promises to return a string.

What happens on line 38?
returns nothing..
should i make it a void function vs a string
It doesn't return nothing. It tries to construct a string with your 0 (null pointer) and fails.

You can, of course, change the design to whatever you wish, but returning an empty string seems appropriate enough to me:

return std::string();

1
2
3
4
5
	while (!infile.eof()) {
		P = scan(line, infile);
		if (!infile.eof() && !p.empty())
		        cout << P << endl;
	}
Thanks cire it fixed that issue,

but i am trying to store the string into an array and i am getting
Error: Access violation writing location(takes me into i believe assembly language )
1
2
3
4
5
6
7
//the error occurs within this function
//this function is within a while loop, whereas int i is being counted outside of the function
void arrayStow)string hold, int i){
string arr[7];
arr[i]=hold;
cout<<arr[i]<<endl;
}
Last edited on
but i am trying to store the string into an array and i am getting

That is obviously not the code you're actually using. arr is local to arrayStow and ceases existing when the function returns.
cire thank again, but
1. when i run my prgram it prints the data i want with line spaces from the times i used getline and did not "==" what i requested. can i delete that space? the problem is I want to store the string into an array but just the desired string, now it stores what i wanted plus the empty strings into my array.
can i delete that space?

Just don't store the empty strings.

1
2
3
4
5
6
7
8
	while (!infile.eof()) {
		P = scan(line, infile);
		if (!infile.eof() && !p.empty())
		{
		        cout << P << endl;
			arrayStow(...);
		}
	}
cire, Thank you
Topic archived. No new replies allowed.