0

I want to read file line to line , here is code :

map<int,string>WordList ; //int is the key, string the returnad value
int GetWordList(char* file)
{
    WordList.clear();
    char getch;
    int wordindex=-1;
    string tempstring="";
    ifstream myFile(file);
    while (!myFile.eof())
    {
         myFile.get(getch);
         if (getch=='\r') continue; // skipping '\r' characters
         if (getch == '\n' || myFile.eof() )
         {
               WordList[++wordindex]=tempstring;
               tempstring="";
         }else  tempstring+=getch;
    }
    return wordindex; //returns the maximum index
}

I have called

 int totalStudents = GetWordList("C:\Students.txt");

I have three line in that file , but when I run program , it will not exit from while loop and also WordList is always 0 ,

Ata
  • 10,914
  • 19
  • 53
  • 94

3 Answers3

4

Given that you're using consecutive integers as the indexes, there seems to be little reason to use std::map<int, string> instead of just std::vector<std::string>.

Likewise, your code for parsing the input into lines seems to accomplish little that std::getline can't already do quite nicely as well.

Finally, your test for end of file isn't really correct. Putting those together, you get something like.

std::vector<std::string> lines;

std::string line;
std::ifstream myFile(filename);

while (std::getline(myFile, line))
    lines.push_back(line);

You might also want to look at some of the answers to a previous question.

Community
  • 1
  • 1
Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
  • @Ata: You probably didn't include the necessary header(s). – Jerry Coffin Jun 12 '11 at 07:55
  • 1
    @Ata: Are you, perhaps, using some hideously ancient compiler? `std::getline` was added more recently than `istream::getline`, but it's been there for well over a decade now... – Jerry Coffin Jun 12 '11 at 08:01
1

Here goes again: Do not test against eof.

Next, why do you make your loop so complicated if you always want to read in exactly one line? There's std::getline for that. Build your loop around that and you should be fine.

Community
  • 1
  • 1
Xeo
  • 123,374
  • 44
  • 277
  • 381
0

Don't forget to escape the backslash:

GetWordList("C:\\Students.txt");
Yakov Galka
  • 61,035
  • 13
  • 128
  • 192
  • -1: the correct way to handle this is to use forward slashes - yes, even on Windows. The Standard guarantees that forward slashes in a path name (e.g. used for the constructor of an fstream object) will be translated into the appropriate path separator for the platform. – Karl Knechtel Jun 12 '11 at 10:03
  • 1
    @karl: What standard guarantees that? – Jerry Coffin Jun 12 '11 at 10:41
  • There is only one Standard that is relevant here - hence the capital letter - the one that specifies and defines the language (and the standard library). That said, I can't quote chapter and verse, but the C++ FAQ Lite backs me up: ["This is because the library routines on these operating systems handle "/" and "\" interchangeably."](http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.16) – Karl Knechtel Jun 12 '11 at 10:44
  • @Karl: The C++ standard says that `basic_filebuf` should function "as if by calling `fopen`". The C standard defines `fopen` as: "The fopen function opens the file whose name is the string pointed to by filename, and associates a stream with it." and says nothing about what the term "name of a file" means. In fact, C and C++ used on platforms where filesystem doesn't exist, in which case there are no paths and these strings are just keys to some database. – Yakov Galka Jun 12 '11 at 11:47
  • @Karl: also your downvote is irrelevant, his code was wrong (among other things) because `\S` is not a valid escape. MSVC compiler just ignores the backslash in that case. – Yakov Galka Jun 12 '11 at 11:49
  • @Karl: and as you see there are at least three standards relevant here: C++, C, and POSIX. What you're talking is about POSIX, which has it's own path format. But Windows is not POSIX compliant, although tries to be so. – Yakov Galka Jun 12 '11 at 11:51
  • @Karl: if you're going just by the C or C++ standard, then you're just plain wrong. Yes, Windows will deal with a forward slash, but it's *not* required by the standard. Other systems have used ":", ".", and various other characters, and wouldn't accept either '/' or '\'. The standard just says (C99, §7.19.3/8): "The rules for composing valid file names are implementation-defined." The current C++ standard refers to the older C standard, which has identical language in §7.9.3. – Jerry Coffin Jun 13 '11 at 16:21
  • So who wants to go tell Mr. Cline about this, then? If it's Windows dealing with it, rather than the C++ runtime, then "the library routines on these operating systems" is inaccurate and/or misleading. – Karl Knechtel Jun 13 '11 at 16:28
  • @Jerry BTW, given that you apparently knew what Standard I had in mind, have access to it (draft versions are hard to find/version-check properly, and final versions aren't even available free) and believed it didn't actually say what I understood it to say (and this is probably a very common situation because of the above), IMO it's kind of a dick move to ask "what standard?" and set a trap. – Karl Knechtel Jun 13 '11 at 16:32
  • @Karl: Yes, it's Windows, not the RTL (and MS-DOS did the same). I didn't know what standard you had in mind until you said. – Jerry Coffin Jun 13 '11 at 16:42