Thread: Read a text file and respond

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    9

    Read a text file and respond

    I tried to make a program that would take a txt file and respond to it. Found some tutorial and used the code from there with some modifications.
    But when I run it, it just opens the dos with nothing in it.

    Here's the code:

    Code:
    //This program should take a text file named VCC.txt and read it, put it to a string 
    //and then if it matches the given string, it should respond
    
    //Includes
     #include <fstream>
     #include <iostream>
     
    
     using namespace std;
     
        int main()
        {
            //The string that the text file will be read to
            char textfile[2000];
            
            //The file
            fstream file_op("Data\\VCC.txt",ios::in);
            
            //Get line and put it to char textfile
            file_op.getline(textfile,2000);
            
            //If textfile is Hello, wich it is, it should respond with saying Hello back, but somehow this doesn't work
            if(textfile == "Hello")
            {
                   cout<<"Hello";
            }
                  
            
            while(1){}
              
        }
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(textfile == "Hello")
    Use strcmp() to compare char array type strings
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use the C++ string class instead of C style strings.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I agree! Then you change this line,

    file_op.getline(textfile,2000);

    to:

    getline( file_op, StringVariable );

    If there is a lot of lines in the text file you can just declare a dummy variable and loop the above line:

    Code:
    string Temp, FileDetails;
    
    while ( getline( in, Temp ) )	// in is like your fstream
    {
    	FileDetails += Temp;	// The line of the file you are wanting.
    	FileDetails += "\n";	// Appends a new line to it (getline gets a line)
    }

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    Quote Originally Posted by twomers
    I agree! Then you change this line,

    file_op.getline(textfile,2000);

    to:

    getline( file_op, StringVariable );

    If there is a lot of lines in the text file you can just declare a dummy variable and loop the above line:

    Code:
    string Temp, FileDetails;
    
    while ( getline( in, Temp ) )	// in is like your fstream
    {
    	FileDetails += Temp;	// The line of the file you are wanting.
    	FileDetails += "\n";	// Appends a new line to it (getline gets a line)
    }

    Alright, I use the strcmp, and now it works, but I still have a problem with the C++ style strings.
    How to use the getline()?
    Could you give me an example, please?

    I don't need the rest because the textfile will only have one line in it.

    I feel like such a n00b right now. Maybe I am one.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Ok, if you want to use getline for cin, you use it like this:

    Code:
    getline( cin, StringVariable );
    where StringVariable is a string type defined like so:
    Code:
    string StringVariable;
    However, if you want to use if for File IO, I would just do what I said in my previous post, and replace the cin with b_file or whatever it was you had.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-27-2008, 03:39 PM
  2. how to read data from 15 pin connection?
    By Euphorica in forum Networking/Device Communication
    Replies: 0
    Last Post: 06-27-2008, 03:38 PM
  3. If you must port to .NET 2005, read this thread.
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-22-2007, 06:51 AM
  4. Input file stream problems
    By Shadow12345 in forum C++ Programming
    Replies: 10
    Last Post: 10-06-2002, 06:33 PM
  5. file i/o-link list
    By new2c in forum C++ Programming
    Replies: 0
    Last Post: 12-14-2001, 03:05 PM