Hi, I am trying to read an input text file. And read the last letter only and check if it is an '+' or '*'.

Here is the code I have written so far, if anyone can help me, I would be very greatful.

while(input>>lastChar) //put file contents into lastChar
  {
	  input.seekg(ios::end,lastChar);  //go to the end of file
	  if(lastChar=='+'  || lastChar=='*') //check if it is + or *
	  {
	  		badInput=true;
		goto skip;
	  }
	  else
	  {
		cout<<"true file"<<endl;
		goto startHere;
	  }
  }

Dani AI

Generated

Brief, practical plan to get the last meaningful character from a file (and check for '+' or '*') without relying on goto.

The usual failure modes are: the file is empty, the last byte is a newline or other whitespace, platform newline translation (CRLF) or multi-byte encodings hide the actual final character, and formatted extraction can skip whitespace unexpectedly. and were right that the stream position must be moved; 's suggestion to add checks and debug output is also worth following.

Simple, reliable workflow (no full code here — implement with your preferred I/O functions):

  1. Open the file and verify it opened successfully. If empty, handle as a special case.
  2. Work from the end of the file backwards, reading raw bytes (or disable whitespace skipping) until you find a non-whitespace byte. Treat '\n' and '\r' as whitespace to skip; on Windows you may see a '\r' before '\n'.
  3. When you reach the first non-whitespace byte, compare it to '+' or '*'.
  4. For small files an easy alternative is to read the whole file into a string, trim trailing whitespace, and inspect the last character — but avoid this for very large files or when memory is constrained.
  5. If you need byte-accurate seeking, prefer binary mode or query file size via the filesystem API before doing byte offsets.

Debugging checklist (follow ): print file size and the last few bytes (in hex or numeric), confirm the stream position before and after seeks, and log whether reads are skipping whitespace. If the file may contain UTF-8 or other multi-byte encodings, operate on characters with proper decoding rather than raw bytes.

Further reference: for reading raw characters including whitespace use the noskipws manipulator or an unformatted read; see std::noskipws (). For obtaining a byte count before seeking, std::filesystem::file_size can help (https://en.cppreference.com/w/cpp/filesystem/file_size).

Recommended Answers

All 3 Replies

To get the last letter from a file you would use

//...
ifstream fin("input.txt");
fin.seekg(ios::end);
char ch;
fin >> ch;
if (ch == '+' || ch == '*')
//...

Also it is bad practice to use the goto command

I thought this looked familiar, then when I went and looked it was your thread.

You're not using seekg() correctly. The second parameter is the seek-direction (position really, ios::end in this case), while the first is an offset from that position (in your case 0, or possibly -1, see what works on a file with known content). cplusplus.com is a great reference.

Now think about what your loop needs to do, and the order in which it needs to do it. And what's with the goto statements? You're not programming in BASIC any more.

P.S. Apparently Nathan was answering at the same time I was! :)

And I'm still waiting for you to

1) Place comments on each line of your code that explains why that line is there. "Because someone said so" is not acceptable.
2) Write down the steps needed in detail to check the beginning of the file for your bad file indicators
3) Start outputting values to see if each variable, each array element, etc contains what you think it should -- based on #2

I didn't suggest this to be mean. I suggested this so you can learn something. It will help.

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.