Click here to Skip to main content
15,610,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to find the line number and position number in a line of a given character in a text file. I did, but I need to read the character not from the beginning, but from the end.
I have a task to find the first point "." in a text file, but in order to search from the end of the file, I have done a search from the beginning of the file, how to do it from the end?
Text file:
One One
....
Two
Three
Four
..

Now I get the following result:
Character position: 8
Line: 2

What I have tried:

Check out my code (I'm looking for the first . in the file):
C++
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <fstream>
#include <string>

using namespace std;

int Search_position(int& i)
{
    ifstream fin("t.txt"); // file name
    char line;
    int k = 0;
    while ((line = fin.get()) > 0) 
    {
        if (line == '.') 
        {
            k++;
            if (k == 1) 
            {
                return i;
            }
        }
        if (line != '\n')
            i++;
    }
    cout << "No";
    return 0;
}

int Search_line(int& n, int& i)
{
    char line;
    int l = 0;
    ifstream fin("t.txt");
    while ((line = fin.get()) > 0) 
    {
        if (line != '\n')
            l++;
        if (l <= i && line == '\n')
            n++, l++;
    }
    return n;
}
int main()
{
    ifstream fin("t.txt");
    char line[150];
    while (!fin.eof())
    {
        fin.getline(line, sizeof(line));
        cout << line << endl;
    }
    int i = 1;
    int n = 1;
    cout << "\nCharacter position : " << Search_position(i) << endl;
    cout << "\nLine : " << Search_line(n, i) << endl;
    return 0;
}
Posted
Updated 10-Apr-22 4:35am

So you need to report the distance from the end of the file of the last occurrence of the '.'.
Try
C++
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    ifstream fin("t.txt");
    string line;

    int lines = 0;
    int characters = 0;

    int found_line = 0;
    size_t found_pos = string::npos;

    while (true)
    {
      if ( ! getline(fin, line) ) break;
      auto pos = line.find_last_of('.');
      if ( pos != string::npos)
      {
        found_line = lines;
        found_pos = (characters + pos);
      }
      ++lines;
      characters += line.length();
    }

    if ( found_pos != string::npos)
    {
      found_line = (lines - 1 - found_line); // line no, counting backwards from the bottom of the file
      found_pos = (characters - 1 - found_pos); // pos of the 'first' dot, counting backwards from the end of the file
      cout << "line " << found_line << "\n";
      cout << "pos " << found_pos << "\n";
    }

    return 0;
}
 
Share this answer
 
Comments
Jonson123 2022 10-Apr-22 13:21pm    
I tried this but got the following result:
line 0
pos 0
CPallini 10-Apr-22 13:30pm    
That's correct: Last dot is at the end of the file.
Jonson123 2022 10-Apr-22 13:33pm    
ok, thanks for your time for my help
CPallini 10-Apr-22 16:27pm    
You are welcome.
Jonson123 2022 11-Apr-22 11:53am    
Sorry, and how and whether it is possible to find 4 points from the end with the help of this code?
Technically, you can't - text files are just a stream of characters, some of which are interpreted by software as an "end of line" character.

So finding any specific character position in a file doesn't help you to learn what line it was on, or even where the line it is on actually starts - because lines aren't a fixed length, and can be from zero up to millions of characters long.

The only way is to read the file as lines counting as you go, and then search each of the lines for your character, starting with the last line and working backwards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900