Answer the question
In order to leave comments, you need to log in
How to move character by character in a file?
Good evening. Faced such a problem, so I wrote a piece of code that should display all the words on the screen. In this case, my file has the following content: "hello world privet!" . The console outputs only Hello and World - the words privet! - no. What is the problem, tell me please.
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
struct Lexeme {
int start, end;
string lexeme;
};
void gToken(ifstream &fin, vector<Lexeme> &v);
int main()
{
char filename[30];
ifstream fin;
char q;
//cin >> filename;
fin.open("text.txt");
//wToken();
vector<Lexeme> v;
gToken(fin, v);
fin.close();
system("PAUSE");
return EXIT_SUCCESS;
}
void gToken(ifstream &fin, vector<Lexeme> &v)
{
char ch;
Lexeme l;
while (fin >> ch)
{
l.lexeme += ch;
while (fin.get(ch))
{
if (ch != ' ' && ch != '\n')
{
l.lexeme+=ch;
}
else
{
cout << l.lexeme << endl;
l.lexeme = " ";
break;
}
}
v.push_back(l);
}
}
Answer the question
In order to leave comments, you need to log in
Hello,
Not taken into account that a word can end with the end of the file. Should be something like this:
void gToken(std::ifstream &fin, std::vector<Lexeme> &v)
{
char ch = 0x00;
Lexeme l = { 0 };
while (!fin.eof())
{
ch = fin.get();
if (!isspace(ch) && !fin.eof())
l.lexeme += ch;
else if (!l.lexeme.empty())
{
v.push_back(l);
l.lexeme.clear();
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question