Reading file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newguy194
    New Member
    • May 2007
    • 25

    Reading file

    I have written the skeleton for a program which is supposed to dissect a text file by first getting the entire contents of the textfile as a string, then searching that string for a matching first word, then skipping over a delimiter and returning the correct word. For some reason it does not return anything, and I do not see where my error is occuring.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <conio.h>
    using namespace std;
     
    int main(int argc, char *argv[])
    {
        string input = "";
        string output = "";
        string search = "";
        string component = "";
        int x = 0;
        cout<<"input:";
        getline(cin, input);
        fstream file("read.txt");
        while(!file.eof())
        {
        getline(file, search);
        }        
             file.close();
             
        while(component != input)
        {
           if(search[x] == '|')
           {
             component.erase(component.size());
             while(search[x] != '\r')
             {
               x++;
               }
           }
           else{component += search[x]; x++;}
        }
             x++;
             while(search[x] != '\r')
             {
               output += search[x];
               }
               
             cout<<output;
             getch();
        return EXIT_SUCCESS;
    }
  • jwwicks
    New Member
    • Oct 2007
    • 19

    #2
    Hello,

    Originally posted by newguy194
    I have written the skeleton for a program which is supposed to dissect a text file by first getting the entire contents of the textfile as a string, then searching that string for a matching first word, then skipping over a delimiter and returning the correct word. For some reason it does not return anything, and I do not see where my error is occuring.

    Code:
        while(!file.eof())
        {
        getline(file, search);
        }        
             file.close();
             
    }
    Your problem is here with the first while. Unless the "input" is on the last line of the text file I dont believe you'll get a match. Try putting all the lines into a vector of strings. getline doesn't append the next line it overwrites search with the new line.

    John

    Comment

    • newguy194
      New Member
      • May 2007
      • 25

      #3
      Originally posted by jwwicks
      Hello,



      Your problem is here with the first while. Unless the "input" is on the last line of the text file I dont believe you'll get a match. Try putting all the lines into a vector of strings. getline doesn't append the next line it overwrites search with the new line.

      John
      Thanks for noticing that problem john, but I am currently testing it with a file where that is exactly the case, so I do not believe that is my main error. Although I was wondering whether simply using
      Code:
        while(!file.eof())
          {
          getline(file, search);
          contents+=search;
          }
      would remedy the problem you noticed.

      Comment

      • newguy194
        New Member
        • May 2007
        • 25

        #4
        I have solved the problems I was encountering by reformatting the file I am using and implementing vectors here:
        Code:
        #include <cstdlib>
        #include <iostream>
        #include <fstream>
        #include <conio.h>
        #include <vector>
        using namespace std;
         
        int main(int argc, char *argv[])
        {
            vector<string> contents;
            string input;
            string output;
            string search;
            int x = 0;
            string component = "";
            cout<<"input:";
            getline(cin, input);
            fstream file("read.txt");
            while(!file.eof())
            {
            getline(file, search);
            contents.push_back(search);
            }
            file.close();
            for(int y = 0;y<contents.size()-1;y+=2)
            {
                    if(contents.at(y) == input)
                    {
                      component = contents.at(y+1);
                      }
            }
            cout<<component;
            getch();
            return EXIT_SUCCESS;
        }

        Comment

        Working...