Just wondering if i can re-read something i just read in?

if(in_stream.is_open())
		while(i<50 && in_stream.getline(temp, 50) && temp>0)
		{
			if(isdigit(temp))
				 PX1>>temp;
			else length=strlen(temp);

			if(length>1)
				 {
				 string1[]=temp;
				 in_stream.getline(temp, 50)
					 if(isalpha(temp))
						 string2[]=temp;
					 else 
				 }
			else PX2>>temp;			i++;
		}
	in_stream.close();

Hope it doesn't come across as vague but if towards the end of a while loop i read in the line containing Novice just to check if it matches something the operation could continue on with (however it doesn't) is it possible to move the line 'indicator' back so when the program falls back to the beginning of the loop it will read in that same line (as i will need that line of data)?

data i.e:

  • 2

  • Lubriderm

  • Novice

  • 3

  • X

  • 6

  • Willis

Recommended Answers

All 4 Replies

Streams have a number of methods that may be useful. For example peek() allows you to look at the next char in the input stream without removing in from the stream, putback() allows you to put last char read from input stream back into to the input stream, seekg() resets the file pointer to a desired space in the file for reading and tellg() returns the current location of the file pointer for reading from file (seekp() and tellp() are available for writing to file as well).

>>while(i<50 && in_stream.getline(temp, 50) && temp>0)
Under what conditions would temp ever be 0 ? My guess is that it can't every be 0 because it is allocated pointer or a character array. So checking for temp > 0 is superflous.

>>if(isdigit(temp))
That is only checking if the first character is a digit. Is that what you want?

>>string1[]=temp;
Syntax error. how is string1 defined. If its just a character array then you have to call strcpy() to copy the contents of temp into string1 array. Assignment operators don't work on character arrays like they do on std::string objects.

>>if(isalpha(temp))
>> string2[]=temp;

Same comments as above for isdigit() and string1.

Yea i forgot to mention when i posted that i was aware it was buggy and poorly written, i was just looking to see what the best way to go about it was. I ended up using peek(), however, even then it's not reading in exactly what i wish. Take a look at this and tell me if you see anything wrong in here. Some logic error in this portion of my program is making the whole thing go to hell, as it's not always reading in the right data into the right variable, etc

char INSTR=4, i=0;
	char PX2;                 
	char temp[SIZE_t];
	char string1[SIZE_t];           //Character string.
	char string2[SIZE_t];           //Character string.
	
	Header();
	
	ifstream in_stream("data4.txt", ios::in);
	if(in_stream.is_open())                               		
                       while(i<50 && !in_stream.eof())  
		{	
			in_stream>>INSTR;

			in_stream.getline(temp, SIZE_t);
			int length=strlen(temp);
			
			if(length>1)
				 strcpy_s(string1, temp);
			else PX2=temp[i];

			temp[i]=in_stream.peek();
			if(isalpha(temp[i]))
				 {
				 in_stream.getline(temp, SIZE_t);
				 strcpy_s(string2, temp);
				 }


	ofstream output("output.txt", ios::out | ios::app);               
	switch(INSTR) {   
           case '0':	output<<"****************************************"<<endl;
	output<<"Converting to all uppercase letters: "<<endl;   
	output<<string1<<"."<<endl;
                for(int ii=0; string1[ii]!='\0'; ii++)						         string1[ii]=toupper(string1[ii]);					output<<string1<<"."<<endl;							output<<"****************************************"<<endl<<endl;
							break;

and so on....

half of data file to give you an idea of where i'm going with this

9
Lubriderm
Lubriderm
1
RECURSION
0
When in the course of human events
4
v
2
y
3
w
7
Spring
Break
9
Verbatim
normal

figured it all out, was just a few more syntax errors. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.