im having this weird situation - im trying to read words from a file.
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
int main(int argc, const char* argv[]) {
if (argc != 2) {
std::cout << "bad number of arguments" << std::endl;
return 1;
}
std::cout << "trying to open file: " << argv[1] << std::endl;
ifstream fin(argv[1]);
std::cout<< "this is a test string" << std::endl;
if (!fin) {
std::cout << "Could not open file" << std::endl;
return 1;
}
string word;
while (fin >> word) {
std::cout << word << std::endl;
}
return 0;
}
the crashing input is: (the words dont make logical sense, just bunch of words)
the man row in front turned when he heard his name
called but had no idea who called him
and was worried maybe police were looking for
so ran hid car
lock master frame card muffin
pancake their pasta shuttle glass
remote where answer chair table
laptop phone key window paper abcdefghijkmop
the file name is being passed as parameter (obviously) and the file exists for sure at the correct location.
my file has has 50 words in it. trying to open the file causes it to crash, it doesnt even get to print the test string i put.
the weird thing is - when i remove some words (leave like 36 words only), it works. when i add even one more word - it crashes.
i tried to check formatting, spaces and newlines, used several text editors - nothing works. as you can see, its first in the main, so i guess nothing there could cause this issue.
i tried checking for other threads like this or this but no success in finding a solution.
please help me solve this issue - drives me crazy that adding a word past the 36ish words in the file makes the program crash, but with that number of words or fewer - it works as expected. i sense its some formatting bug that i miss, but i just dont know what.
im using windows 7, visual studio 2013 if that helps.
UPDATE: i tried another example with longer words, and i noticed that when i remove the longer words, it works, so perhaps the problem was with word length. however, i used "string word". could that possibly be a problem? (by longer i mean like "population", not something extreme)