cin a text file

This is a discussion on cin a text file within the C++ Programming forums, part of the General Programming Boards category; In the following example, how would I change it to have the user input the file name? Thanks! #include<iostream.h> #include<fstream.h> ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    74

    cin a text file

    In the following example, how would I change it to have the user input the file name? Thanks!

    #include<iostream.h>
    #include<fstream.h>

    void main()
    {
    ifstream in;

    in.open("c:/assignment.txt");

    char inchar;

    in.get( inchar );

    while(!in.eof()){
    cout << inchar;
    in.get( inchar );
    }

    in.close();
    }

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    #include<fstream.h>

    void main()
    {
    const int FILE_LEN=100;
    char FileName[FILE_LEN];

    ifstream in;

    cout << "Please enter filename: ";
    cin.getline(FileName, sizeof FileName);

    in.open(FileName);

    char inchar;

    in.get( inchar );

    while(!in.eof()){
    cout << inchar;
    in.get( inchar );
    }

    in.close();
    }
    Be a leader and not a follower.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    74
    Thanks!!!

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    74
    Ok, now need to search that file for a string of characters. My code always gives me a "yes", and print out ]] (or something similar) for the "target".

    #include<iostream.h>
    #include<fstream.h>

    int top;

    void search(char data[5], char target)
    {
    if (data[top++] = target)
    cout << "Yes, pattern " << target << " is found." ;

    else
    cout << "Not found" ;
    }

    void main()
    {
    const int FILE_LEN=100;
    char FileName[FILE_LEN];

    ifstream in;

    cout << "Please enter filename: ";
    cin.getline(FileName, sizeof FileName);

    in.open(FileName);

    char inchar;
    in.get( inchar );

    while(!in.eof()){
    cout << inchar;
    in.get( inchar );
    }


    char pattern [5];
    cout << "Enter pattern: " ;
    cin.getline (pattern,5);

    search(FileName, pattern[5]);

    in.close();
    }

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    what a mess, worst than fountain's.
    Be a leader and not a follower.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    74
    thanks subdene, I know. I have big problems with search. Can you help!!!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >if (data[top++] = target)
    You need two = sign to do a comparison. Single is assignment. Make it
    >if (data[top++] == target)

    The rest of it looks a bit wrong to me! What are you trying to search, the file contents or the filename? you are passing the filename to search(), which isn't actually doing a search anyway?!

    In this call:
    >search(FileName, pattern[5]);
    What are you expecting to pass as the second parameter? You do realise that it's only a single character, even then it's not one that's within the array bounds of pattern. Look, you declared it as
    >char pattern[5];
    ... therefore the highest index you have is pattern[4], remember it starts at 0.

    What are you using the top variable for?

    And don't use void main(), its wrong, use int main(void).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Try this.

    Code:
    #include <fstream.h>
    
    int Search(const char [], const char [], ifstream &, char []);
    
    int main(void)
    {
      const int BUFF_LEN=100;
    
      char FileName[BUFF_LEN], Pattern[BUFF_LEN], Buffer[BUFF_LEN];
      int PatternCount;
    
      ifstream File;
    
      cout << "Please enter file name: ";
      cin.getline(FileName, sizeof File);
    
      File.open(FileName);
    
      if(!File)
        cout << "Could not open file....";
      else
      {
        cout << "Please enter the patten you want to look for: ";
        cin.getline(Pattern, sizeof Pattern);
    
        PatternCount = Search(FileName, Pattern, File, Buffer);
    
        cout << "The file " << FileName << " has the word(s) " << Pattern << " "
             << PatternCount << " times.";
      }
      cin.get();
      return 0;
    }
    
    int Search(const char FileName[], const char Pattern[], ifstream &File,
               char Buffer[])
    {
      int Occurances=0;
    
      while(!File.eof())
      {
        File >> Buffer;
        if(strcmp(Buffer, Pattern)==0)
          Occurances++;
      }
    
      return Occurances;
    }
    Be a leader and not a follower.

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    74
    Thanks, subdene. Exactly what I was looking for!!

  10. #10
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    As long as you can learn from it and understand it.
    Be a leader and not a follower.

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    74
    Yes, subdene I am learning. I found some bugs. Your program was returning yes not matter what. I've revised it to return yes when found and no when not, but it's returning many many yes's and one no - all at same time. Need to do some type of update to compare say 5 char's at a time, if it's a match return yes, if no match continue searching until match or eof. Any ideas?

  12. #12
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Are you ........ed? How can it return yes when it is of type int? The idea was, that if the user entered a word that was not found in the file it would just output o occurances.
    Be a leader and not a follower.

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    74
    no i'm not ........ed. why would you get that idea??

    sorry, i realized that after i posted, about the zero times. I appreciate your help!

    I'm still fooling with it, but it does pretty much what I wanted from what I could see.

    do you forgive me

  14. #14
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    sorted.
    Be a leader and not a follower.

  15. #15
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    Originally posted by subdene
    what a mess, worst than fountain's.

    hehe in your dreams sub
    Such is life.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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