Thread: fstream question

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    I've tried strcat, but it wouldn't let me do it cause the 2nd peramiter needed to be a const char

  2. #17
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    string::c_str() returns the char* of the std::string

  3. #18
    Registered User
    Join Date
    May 2002
    Posts
    317
    Hey, for what your doing, just use this:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    
    main()
    {
    	char x;
    	string current; //change to string
    	ifstream infile;
    
    	infile.open("text.txt",ios::in);
    
    	while(infile)
    	{
    		infile.get(x);
    	current += x;
    	}
    
    	return 0;
    }
    This is the easiest way to accomplish the task you are doing.

  4. #19
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    when I did that, this is the errors I got:
    Code:
    "\readtxt.cpp(10) : error C2065: 'string' : undeclared identifier"
    "\readtxt.cpp(10) : error C2146: syntax error : missing ';' before identifier 'current'"
    "\readtxt.cpp(10) : error C2065: 'current' : undeclared identifier"
    I did exactly what you did, any more ideas on what's going on?

  5. #20
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    did you include string.h?
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  6. #21
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    sure did

  7. #22
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    What kind Of C++ do you use

    What is the C++ compiler that you have ?
    C++
    The best

  8. #23
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    thanks for all your guys' help, I got to work

    YAY for me
    Last edited by Agent89; 06-04-2002 at 03:02 PM.
    Newbie @ Work

  9. #24
    Registered User
    Join Date
    May 2002
    Posts
    317
    So what was the problem? I'm curious.

  10. #25
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    I'm not really sure.

    What I did was did a seperate infile.get and then I was able to strcmp them. I don't know exactly, it's at school, I'll let you know more tomorrow
    Newbie @ Work

  11. #26
    Registered User
    Join Date
    May 2002
    Posts
    317
    All right, thanx. I'd really like to know.

  12. #27
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    When you're dealing with arrays to store data in almost any situation, a loop is always the answer. To quot from one of the greatest films of all time, the advice of Dr. Leo Marvin, Bob Wiley's, psichiatrist:
    Baby steps. Don't think of how you're going to get out of this building, concentrate on how you're going to get out of this room. And once you're in the hall, deal with that hall, and so on.

    That's a funny movie..

  13. #28
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    yeah, I'll post my code that works on thursday
    Newbie @ Work

  14. #29
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    Code:
    #include <iostream.h>  //Original Idea for question code By Jeremy Jones
    #include "apstring.h"
    #include <fstream.h>
    #include <string.h>
    
    int main()
    {
    	char x;
    	char answer[20];
    	ifstream infile;
    	char correct[13];
    	bool answered;
    
    
    
    	infile.open("trivia.dat", ios::in); //opens the file
    
    	while (infile.get(x))
    	{
    	if(x == '|') //pauses the file read for the user to answer
    	{
    	cin >> answer;
    	answered = true; //sets the boolian for the fact that the user has answered the question
    	}
    	else 
    	{
    		cout << x;
    	}
    	
    	if(answered == true) //this checks to see if the user has answered the question.
    	{
    		infile.get(correct,13); //grabs the characters
    		infile.ignore(80,'\n'); //puts them into a string
    
    	if(!strcmp(correct,answer)) //this checks ifthe user answer is correct.
    	{
    		cout<<"\nCorrect!!\n"; //tells the user he's right
    	}
    	
    	else
    	{
    		cout << "\nSorry, that's incorrect\n"<<endl; //tells the unfortunate user the bad news
    	}
    	}
    	
    	}
    	return 0;
    	}
    Newbie @ Work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. FStream Question
    By CPP-Null in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2003, 01:28 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. fstream question!
    By FutureCoder in forum C++ Programming
    Replies: 1
    Last Post: 01-31-2003, 04:06 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM