0

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)

Community
  • 1
  • 1
Zephyer
  • 333
  • 6
  • 16
  • [We need a **complete** example](http://stackoverflow.com/help/mcve): a full program and the input that causes the bad behavior. – Cornstalks Mar 09 '15 at 04:19
  • will update post in a moment – Zephyer Mar 09 '15 at 04:23
  • Technically your main is non-standard as the second parameter must be `char* argv[]` – Neil Kirk Mar 09 '15 at 04:23
  • Would be good to know operating system, compiler, level of optimization, and if you've run it with a debugger where does the debugger say your program crashed - if you haven't run it on a debugger, why not. – amdn Mar 09 '15 at 04:25
  • please read updates. thank you – Zephyer Mar 09 '15 at 04:35
  • 1
    I cannot reproduce the error. The code works for me. – Beta Mar 09 '15 at 04:39
  • 1
    Which header files are you including? Anything non-standard? – amdn Mar 09 '15 at 04:41
  • Beta - thank you. please try the updated example i put (the only difference is the relatively long word in the end). please tell me if that works. amdn - added my includes. nothing non-standard – Zephyer Mar 09 '15 at 04:41
  • 1
    Yes, it still works. But on your system the code crashes even without that long word, correct? And if it crashes before control reaches the test string, have you tried removing the code after the test string? – Beta Mar 09 '15 at 04:45
  • 1
    Try removing you don't need it. – amdn Mar 09 '15 at 04:45
  • i figured it crashed on "longer" words when i tried a file with "a b c d ...." and it worked. then a long "abcdeashklhsdfh" and it crashed. right now i tried replacing "string word" with char word[1024] and it works. i dont understand why "string word" was limited, though. can you guys explain? edit - the stdio.h removal solved the issue. i guess it somehow limited the "string word" length. please post as answer so i can accept. will upvote some replies as well. thank you! – Zephyer Mar 09 '15 at 04:45
  • 1
    Do you have "using namespace std" or "using std::string"? – amdn Mar 09 '15 at 04:47
  • I also cannot reproduce the problem, but I did have to make changes (using namespace std) to the code to compile. You should make sure your example is complete. – Retired Ninja Mar 09 '15 at 05:07

1 Answers1

0

Since you are using C++ you should stick to C++ standard library I/O headers:

#include <iostream>
#include <fstream>

The old-style C header is

#include <stdio.h>

The C++ compatibility header for "stdio.h" is

#include <cstdio>

For some reason that I do not understand mixing <iostream> and <stdio.h> causes problems under Visual Studio 2013.

I'm glad removing <stdio.h> fixed your problem, even if it was just a hunch and I can't enlighten you more with a root cause.

amdn
  • 11,314
  • 33
  • 45