fstream

This is a discussion on fstream within the C++ Programming forums, part of the General Programming Boards category; Code: ifstream nounlist("noun1.ygf"); if (!nounlist) cerr << "Problem: noun1list doesnt exist or is being used\n"; nounlist >> temp; cout << ...

  1. #1
    ygf_person
    Guest

    fstream

    Code:
    ifstream nounlist("noun1.ygf");
    if (!nounlist)  cerr << "Problem: noun1list doesnt exist or is being used\n";
    
      nounlist >> temp;
      cout << temp << "   " << temp.length() << "    " << psz << "   " << psz.length() << endl;
    
      flag+=(temp==psz);
    noun1.ygf has one word per line, about 500 or so words
    temp and psz are std string objects
    psz contains some word that is being checked against the word list, lets say "shirt"
    normally it would use a while loop to check each one, but i've taken it out to make the problem more noticable. it only checks one
    here's the problem: the command cout << .... prints twice:
    Code:
    account       7        0
    account       7        shirt      5
    then it continues
    why does it do that, and how can i stop it?

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    dang, i logged in wrong. that's me, above
    the output should look like this. in the above post the spaces are a little off
    Code:
    account       7           0
    account       7        shirt      5

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    since they are strings, this statement will not work.
    flag+=(temp==psz);


    Instead try:
    flag+=(!strcmp(temp,psz));


    The only problem with that is that the not symbol does not have to return 1. So you might want to try


    flag+=((!strcmp(temp,psz))?1:0)

    I think that should do the trick.

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    psz and temp are not char* strings, they are string strings
    i think that it would work with psz==temp, correct me if i'm wrong.
    but the real problem is, why does cout print that and not just one line?

    ideal output:
    Code:
    account    7   shirt   5
    Last edited by ygfperson; 02-19-2002 at 01:14 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >since they are strings, this statement will not work.
    If these were C strings that would be the case, the C++ string class is far more convenient.

    >i think that it would work with psz==temp, correct me if i'm wrong.
    You're correct

    >why does cout print that and not just one line?
    You only have one print statement in this case so the problem is in how you fill the string, step through your code and look at the values of your strings. Does this happen with every iteration of the code you posted? If so then it's definitely something up with your assignment to the strings, otherwise I'd have to see more code to be sure, preferably something that I can compile and test.

    -Prelude
    My best code is written with the delete key.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    guess you learn something every day.

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <string>
    #include <fstream>
    
       int word_count(string);
       int parse(string);
       string word(int,string);
       int word_identify(string);
    
    int main()
    {
      string input;
      getline(cin,input);
      parse(input);
    
    
     return 0;
    }
    
    int parse(string input) {
       int word_type=0;
    
    if (input.at(0)!='#') {
     int word_type=0;
     for (int count=0;count<word_count(input)+1;count++) { word_type+=word_identify(word(count,input)); }
    }
    cout << endl << word_type<<endl;
    return 0;
    }
    
    int word_identify (string psz) {
       int flag=0;
       string temp;
       ifstream nounlist("noun1.ygf");
       if (!nounlist)  cerr << "Problem: noun1list doesnt exist or is being used\n";
    
      nounlist >> temp;
      cout << temp << "   " << temp.length() << "    " << psz << "   " << psz.length() << endl;
    
      flag+=(temp==psz)?1:0;
    
    
    return flag;
    }
    word(int,string) is a function which returns word number ? from the string
    word_count(string) counts the # of words, separated by whitespace

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for (int count=0;count<word_count(input)+1;count++)

    Why word_count(input)+1 here? I would think it should either be:

    for (int count=1;count<word_count(input)+1;count++)

    or

    for (int count=0;count<word_count(input);count++)

  9. #9
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    you're right. corrected it, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 09:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 11:21 AM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21