Thread: fstream question

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    35

    fstream question

    how do I take characters from a file and put them into a string so I can compare them??

    your help is greatly appreciated

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You can use the get method to put them into a single character.

    http://www.cplusplus.com/ref/iostream/istream/get.html

    And then you can use the += operator for string to concat it to the end of a string.

    However, if you want to compare single characters, you don't need to use strings.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
        std::ifstream input("inputfile.txt");
        std::string s;
        char current;
        while (!input.eof()) {
            current = input.get();
            s += current;
        }
        return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    thank you!

    that's exactly what I was looking for

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    I can't get the += to work

    my compiler only wants =+, and that doesn't work with chars.

    plus it won't let me declare a variable with string.

    please help

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    317
    OK, depending in what compiler you have you may need:
    Code:
    #include<string.h>
    //or
    #include<cstring>
    //or
    #include<cstream>
    One of these options should work.

  6. #6
    =+ is different than +=, because += adds to an integer, where =+ is meaning "equal plus"

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    ok, I can include string.h, but not the others, but I still can't use +=, do you guys want to see my code?

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    =+ really means taht the number after that is positive . post the code.

  9. #9
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    What compiler are you using?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    Visual C++ 6.0

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    317
    You should be able to use +=, so yes please post your code.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    Code:
    //readtext.cpp
    
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    
    
    
    
    main()
    {
    	char x;
    	char current[6];
    	ifstream infile;
    
    	infile.open("text.txt",ios::in);
    
    	while(infile)
    	{
    		infile.get(x);
    	current += x;
    	}
    
    	return 0;
    }

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    i guess i didnt exactly understand. i dont think that would work because would this:
    Code:
    int Int = 5;
    int Array[5];
    Array += Int;
    use strlen():
    Str[ strlen( Str ) ] = Chr;

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    I'm new to programming, can you give me an example on how to use strlen in my situation?

    Thanks

  15. #15
    Unregistered
    Guest
    string current;

    while(infile);
    {
    infile.get(x);
    current += x;

    works because the += operator is overloaded for the STL string class of which current is an instance.

    However, the += operator is not available for Cstyle, null-terminated char array type strings. You need to use strcat() for that.

    //declare an string big enough to hold what you want
    char * current = new char[10000000];
    current[0] = '\0';

    //declare a second string to act as input
    char * input[2];
    input[1] = '\0';

    //read the file one char at a time, placing the char as the first
    //element in the string called input
    while(infile)
    {
    infile.get(input[0]);

    //then concatenate the string called input onto the string called
    //current
    strcat(current, input);
    //etc.

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