Reading files word by word

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JWest46088
    New Member
    • Sep 2006
    • 74

    Reading files word by word

    I can't figure out how to read a file word by word. I want to open the file and then read it one word at a time. For example, I'm looking for words that start with d. How would I scan each word of the file to see if it starts with the letter d?
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by JWest46088
    I can't figure out how to read a file word by word. I want to open the file and then read it one word at a time. For example, I'm looking for words that start with d. How would I scan each word of the file to see if it starts with the letter d?
    After reading the word check whether the position 0 of the string contains d .
    If it is then its the word you are lokking for or else skip it.
    [code=c]
    char arr[] = "defghi";
    if(arr[0] == 'd')
    {
    /*This word starts with d*/
    }
    else
    {
    /*skip it*/
    }
    [/code]

    Raghuram

    Comment

    • JWest46088
      New Member
      • Sep 2006
      • 74

      #3
      Originally posted by gpraghuram
      After reading the word check whether the position 0 of the string contains d .
      If it is then its the word you are lokking for or else skip it.
      [code=c]
      char arr[] = "defghi";
      if(arr[0] == 'd')
      {
      /*This word starts with d*/
      }
      else
      {
      /*skip it*/
      }
      [/code]

      Raghuram
      How would I get the word though? I don't know how to get one word at a time. I only know how to use getline a little bit.

      Here's the code I have:

      Code:
                      if(myfile.is_open())
                      {
                              while(! myfile.eof())
                              {
                                      myfile >> word;
                                      cout << word << endl;
      //                              getline(myfile, word);
      //                              cout << word  << endl;
      //                              getline(myfile,line);
      //                              cout << line << endl;
                              }
                              myfile.close();
                      }
              }
      
              return 0;
      }
      It reads the file and outputs one word on a new line, like this:

      hello.
      my
      name
      is
      joe.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by JWest46088
        if(myfile.is_op en())
        {
        while(! myfile.eof())
        {
        myfile >> word;
        cout << word << endl;
        // getline(myfile, word);
        // cout << word << endl;
        // getline(myfile, line);
        // cout << line << endl;
        }
        myfile.close();
        }
        }

        return 0;
        }


        It reads the file and outputs one word on a new line, like this:

        hello.
        my
        name
        is
        joe.
        I'm sorry, but you are reading the file word by word. Just do what it says in Post #2.

        Comment

        • JWest46088
          New Member
          • Sep 2006
          • 74

          #5
          I wrote more code. I added two linked lists I need and tried to open the file, read a word at a time, and insert it into the linked list. I don't think I did it right though. When I run the program, it asks the user to input a filename like it's supposed to and accepts the response, but after that, nothing happens. The program just keeps running without doing anything. Feedback of what I did wrong and what I have to do to fix it would be appreciated. Thanks.

          Here's my new code:

          Code:
          #include <iostream>
          #include <fstream>
          #include <string>
          using namespace std;
          
          struct dlist
          {
          	char dword[30];		//holds a word up to 30 characters
          	int dcount;		//number of times a word appears
          	dlist *dnext;		//pointer to next node
          };
          
          struct anylist
          {
          	char anyword[30];
          	int anycount;
          	anylist *anynext;
          };
          
          dlist *dhead = NULL;
          dlist *dpos;
          anylist *anyhead = NULL;
          anylist *anypos;
          
          void insert();
          void display();
          
          int main()
          {
          	dhead = NULL;
          	anyhead = NULL;
          	insert();
          	display();
          
          	cout << "test";
          
          	return 0;
          }
          
          void insert()
          {
          //	head = NULL;
          	dlist *dtemp, *dtemp2;
          	anylist *anytemp, *anytemp2;
          	string file, word;
          	
          	dtemp = new dlist;
          	anytemp = new anylist;
          
          	cout << "\nEnter a file to scan: ";
          	cin >> file;
          
          	if(file != "dtext.txt")
          
          		cerr << "\nInvalid file.\n" << endl;
          
          	ifstream myfile("dtext.txt");
          
          	while(file == "dtext.txt")
          	{
          		if(myfile.is_open())
          		{
          		cout << "file open test";
          			while(! myfile.eof())
          			{
          				myfile >> word;
          				
          				if(word[0] == 'd')
          				{
          //					cout << word << endl;
          					dtemp->dword;
          //					dcount++;
          					dtemp->dnext = NULL;
          				}
          				else
          				{
          					anytemp->anyword;
          //					anycount++;
          					anytemp->anynext = NULL;
          				}
          
          				if(dhead == NULL)
          				{
          					dhead = dtemp;
          					dpos = dhead;
          				}
          				else
          				{
          					dtemp2 = dhead;
          
          					while(dtemp2->dnext != NULL)
          						dtemp2 = dtemp2->dnext;
          					dtemp2->dnext = dtemp;
          				}
          			myfile.close();
          			}
          		}
          	}
          
          	return;
          }
          
          void display()
          {
          	dlist *dtemp;
          	anylist *anytemp;
          
          	dtemp = dhead;
          	anytemp = anyhead;
          
          	if(dtemp == NULL)
          		cout << "The list is empty!" << endl;
          	else
          	{
          		while(dtemp != NULL)
          		{
          			cout << "D List: " << dtemp->dword << endl;
          
          			dtemp = dtemp->dnext;
          		}
          	}
          	
          	if(anytemp == NULL)
          		cout << "The any list is empty" << endl;
          	else
          	{
          		while(anytemp != NULL)
          		{
          			cout << "Any List: " << anytemp->anyword << endl;
          			
          			anytemp = anytemp->anynext;
          		}
          	}
          }

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            What is the use of this line

            [code=cpp]
            while(file == "dtext.txt" )
            [/code]
            This will loop continuously.
            Change to if loop or else remove it.


            Raghuram

            Comment

            Working...