1
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
#include <fstream>

    int b = 0;
    ifstream pl("09052021.txt");
    if (pl.is_open()) {
        char c;
        while (pl.get(c)) {
            if (c == '1') {
                b++;
            }
        }
    }
    else {
        cout << "wtf";
    }
    cout << b;
}

Is it possible to read date in c++ from a file at certain point? eg. I want to read 7th letter of the file. here i go through the whole file while i want to read just a part of it.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Urna13
  • 11
  • 1
  • 1
    Your C++ textbook should have a chapter on reading from files, and how to use various `std::ifstream` methods such as `seekg`(). Is there anything in your textbook's explanation that's unclear to you? What you're asking is something that should be covered in every C++ textbook. – Sam Varshavchik May 09 '21 at 16:18
  • well i have no textbook and wasn't able to find a example on the internet. Would you be so kind and make one?@drescherjm – Urna13 May 09 '21 at 16:38
  • 1
    C++ is the most complicated and hard to learn general purpose programming language in use today. It takes on average 3-5 years of learning C++ using an organized, methodical, textbook-guided approach, before becoming proficient in this language. It is not possible to learn C++ from Internet searches, Youtube videos, random web sites, or by asking one question at a time; but only [from a good C++ textbook](https://stackoverflow.com/questions/388242/). Only in a textbook you will find detailed explanation of classes, inheritance, polymorphism, overloading, templates, etc... etc... etc... – Sam Varshavchik May 09 '21 at 16:41
  • sir Iam not looking for a object programing course but a simple method and i dont need a textbook for it... – Urna13 May 09 '21 at 16:47
  • @SamVarshavchik I've been working with C++ for over 20 years (and about 3/4 of that time as a professional programmer), and *never once* have I ever crack open a C++ textbook, and did not study it in school. I learned C++ by asking questions, studying other people's examples, and writing test apps to learn concepts. – Remy Lebeau May 09 '21 at 19:03
  • 1
    @RemyLebeau -- Beethoven was deaf. That only makes his accomplishments greater. It doesn't mean that anyone seeking to become a great composer need to suffer the same disability. – Sam Varshavchik May 09 '21 at 19:08
  • @SamVarshavchik touché – Remy Lebeau May 09 '21 at 19:35

1 Answers1

1
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
#include <fstream>

    int b = 0;
    ifstream pl("09052021.txt");
    if (pl.is_open()) {
        char c;
        pl.seekg(6);
        while (pl.get(c)) {
            if (c == '1') {
                b++;
            }
        }
    }
    else {
        cout << "wtf";
    }
    cout << b;
}

As depicted in the comments, seekg() allows you to set the absolute position of the input indicator, in my example you now start with the 7th character.