I am trying to add each word from a file to a vector but if I make the size of the vector (500) and I only have 20 words in the file. The size of the vector is still considered 500. How do I fix this?
Am I doing this a bad way? Could this be made simpler?
void loadFile(string fileName)
{
vector<string> fileContents(500);
int p = 0;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;
string word;
while (file >> word)
{
fileContents[p] = word;
p++;
}
for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}
You could also use a more direct approach, copying immediately from the input stream.
std::vector<std::string> loadFile(std::string fileName) {
std::ifstream file(fileName);
assert(file);
std::vector<std::string> fileContents;
std::copy(std::istream_iterator<std::string>(file),
std::istream_iterator<std::string>(),
std::back_inserter(fileContents));
return fileContents;
}
#drescherjm in the comments gave me the correct answer.
void loadFile(string fileName)
{
vector<string> fileContents;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;
string word;
while (file >> word)
{
fileContents.push_back(word);
}
for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}
Related
I am trying to read input.txt file, and trying to put each line into the array as string (later on I will use each element of array in initializing obj that's why I am putting each line into the array).
string* ptr = new string;
// Read Mode for Input
fstream input;
input.open("input.txt", ios::in);
int size = 0;
if (input.is_open()) {
string line;
while (getline(input, line)) {
cout << line << endl;
ptr[size] = line;
size++;
}
input.close();
}
for (int i = 0; i < size-1; i++) {
cout << "array: " << ptr[i] << endl;
}
I am getting error as:
Proxy Allocated, drain it
Don't use arrays; use std::vector. The std::vector behaves like an array and uses Dynamic Memory:
std::string s;
std::vector<std::string> database;
while (std::getline(input, s))
{
database.push_back(s);
}
Keep it simple. :-)
As was noted in the comments, if you don't know how many lines in the file then you need a container which grows on request at runtime. The natural choice is std::vector :
std::fstream input("input.txt", std::ios::in);
std::vector<std::string> lines;
std::string line;
while (getline(input, line)) {
lines.push_back(line); // std::vector allocates more memory if needed
}
for (int i = 0; i < lines.size(); i++) {
std::cout << lines[i] << std::endl;
}
Please can you advise, why the inner loop runs only once?
I'd like to add suffix to each line of input file and then store the result in output file.
thanks
For example:
Input file contains:
AA
AB
AC
Suffix file contains:
_1
_2
Output file should contain:
AA_1
AB_1
AC_1
AA_2
AB_2
AC_2
My result is :
AA_1
AB_1
AC_1
Code:
int main()
{
string line_in{};
string line_suf{};
string line_out{};
ifstream inFile{};
ofstream outFile{"outfile.txt"};
ifstream suffix{};
inFile.open("combined_test.txt");
suffix.open("suffixes.txt");
if (!inFile.is_open() && !suffix.is_open()) {
perror("Error open");
exit(EXIT_FAILURE);
}
while (getline(suffix, line_suf)) {
while (getline(inFile, line_in))
{
line_out = line_in + line_suf;
outFile << line_out << endl;
}
inFile.close();
outFile.close();
}
}
IMHO, a better method is to read the files into vectors, then iterate through the vectors:
std::ifstream word_base_file("combined_test.txt");
std::ifstream suffix_file("suffixes.txt");
//...
std::vector<string> words;
std::vector<string> suffixes;
std::string text;
while (std::getline(word_base_file, text))
{
words.push_back(text);
}
while (std::getline(suffix_file, text))
{
suffixes.push_back(text);
}
//...
const unsigned int quantity_words(words.size());
const unsigned int quantity_suffixes(suffixes.size());
for (unsigned int i = 0u; i < quantity_words; ++i)
{
for (unsigned int j = 0; j < quantity_suffixes; ++j)
{
std::cout << words[i] << suffix[j] << "\n";
}
}
Edit 1: no vectors
If you haven't learned about vectors or like to thrash your storage device you could try this:
std::string word_base;
while (std::getline(inFile, word_base))
{
std::string suffix_text;
while (std::getline(suffixes, suffix_text))
{
std::cout << word_base << suffix_text << "\n";
}
suffixes.clear(); // Clear the EOF condition
suffixes.seekg(0); // Seek to the start of the file (rewind).
}
Remember, after the inner while loop, the suffixes file is at the end; no more reads can occur. Thus the file needs to be positioned at the start before reading. Also, the EOF state needs to be cleared before reading.
I want to take more than one line from the data.txt file. I am able to take only the first one. I tried using while loop but it seems that I don't know how to use it in this case.
Edited with while loop:
#include <iostream>
#include <fstream>
using namespace std;
int zapis()
{
fstream file;
string text;
file.open("data.txt", ios::app);
cout << "Type in text that you would like to store: ";
getline(cin, text);
file << text << endl;
file.close();
return 0;
}
int odczyt()
{
fstream file;
string line;
int nr_lini = 1;
file.open("data.txt", ios::in);
if(file.good()==false)
{
cout << "Error occured!";
}
else
{
while(getline(file, line))
{
getline(file, line);
cout << line;
}
}
file.close();
return 0;
}
int main()
{
zapis();
odczyt();
return 0;
}
Why call getline twice in your loop? Also pay attention to the semi-colons
while(getline(file, line));
^
What do you think the semi-colon there does?
This is correct
while (getline(file, line))
{
cout << line;
}
Your code is correct, just loop through the file. Also, you could make the function void, as it always returns 0, with you not doing anything with the return value.
void odczyt(){
fstream file;
string line;
file.open("data.txt", ios::in);
if(!file.good())
{
cout << "Error occured!";
}
else
{
while(getline(file, line);) { // while text file still has lines, you write the line and read next
cout << line;
}
}
file.close();
}
For some reason the full lines from my input file are not reading into the array, only the first word in each line. I am currently using the getline call, but I am not sure why it is not working. Here is the what I have for the call to populate the array. The txt file is a list of songs.
const int numTracks = 25;
string tracks[numTracks];
int count = 0, results;
string track, END;
cout << "Reading SetList.txt into array" << endl;
ifstream inputFile;
inputFile.open("SetList.txt");
while (count < numTracks && inputFile >> tracks[count])
{
count++;
getline(inputFile, track);
}
inputFile.close();
while (count < numTracks && inputFile >> tracks[count])
The >> operator reads a single word. And this code reads this single word into the vector in question.
getline(inputFile, track);
True, you're using getline(). To read the rest of the line, after the initial word, into some unrelated variable called track. track appears to be a very bored std::string that, apparently, gets overwritten on every iteration of the loop, and is otherwise completely ignored.
Your loop is using the operator>> to read the file into the array. That operator reads one word at a time. You need to remove that operator completely and use std::getline() to fill the array, eg:
const int numTracks = 25;
std::string tracks[numTracks];
int count = 0;
std::cout << "Reading SetList.txt into array" << std::endl;
std::ifstream inputFile;
inputFile.open("SetList.txt");
while (count < numTracks)
{
if (!std::getline(inputFile, tracks[count])) break;
count++;
}
inputFile.close();
Or:
const int numTracks = 25;
std::string tracks[numTracks];
int count = 0;
std::cout << "Reading SetList.txt into array" << std::endl;
std::ifstream inputFile;
inputFile.open("SetList.txt");
while ((count < numTracks) && (std::getline(inputFile, tracks[count]))
{
count++;
}
inputFile.close();
Alternatively, consider using a std::vector instead of a fixed array, then you can use std::istream_iterator and std::back_inserter to get rid of the manual loop completely:
class line : public std::string {}
std::istream& operator>>(std::istream &is, line &l)
{
return std::getline(is, l);
}
...
std::vector<std::string> tracks;
std::cout << "Reading SetList.txt into array" << std::endl;
std::ifstream inputFile;
inputFile.open("SetList.txt");
std::copy(
std::istream_iterator<line>(inputFile),
std::istream_iterator<line>(),
std::back_inserter(tracks)
);
inputFile.close();
I want to pass an fstream object and tokenize the the words. It will always print could not open file in hasNextToken() . May someone help me.
//main.cpp
int main() {
string filename = "input.txt";
fstream inputStream(filename);
Tokenizer t(inputStream);
while (t.hasNextToken()) {
cout << t.nextToken();
}
}
//Tokenizer.h
class Tokenizer {
fstream fin;
public:
Tokenizer(fstream& file)
{
fin << file;
}
bool hasNextToken() {
if (!fin) {
cout << "Could not open file: " << endl;
exit(0);
}
return true;
}
string nextToken() {
string line;
getline(fin, line);
if (fin) {
istringstream sin(line);
string word;
sin >> word;
return word;
}
}
};
Try this:
class Tokenizer {
fstream& fin;
public:
Tokenizer(fstream& file)
: fin(file)
{}
...
}
I'm not sure if this will work, I can't test it right now, but you can do it quickly:
int main() {
string filename = "input.txt";
fstream inputStream(filename, ios::in); // add second argument
// other stuff here
}
Cheers