Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Combinding variables... (http://www.programmingforums.org/showthread.php?t=11092)

TCStyle Aug 16th, 2006 12:36 PM

Combinding variables...
 
I'm trying to open 5 files name 1.txt, 2.txt, 3.txt,... and check and see if string "chris" exists in it (the latter isn't scripted yet). I'm stuck on how to open the files.

So I have variable X and it's incremenating until it reaches 5.
:

int x = 1;
string data;
while (x <= 5) {
fstream file;
file.open(x + ".txt");
file.close();
x++;
}

But the files never open...

ReggaetonKing Aug 16th, 2006 1:00 PM

:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
        string word;
        for(int x = 0; x <= 5; x++)
        {
                ifstream in(x + ".txt");
                while(in << word)               
                {
                    //check to see if word matches chris.
                }
        }
        return 0;
}

I think this should work, try it out and post back. I like Overloading Operators.

Narue Aug 16th, 2006 1:02 PM

>file.open(x + ".txt");
That doesn't do what you think it does. You need to convert x to a string object first, then append ".txt" to that string. Finally, because open takes a c-style string, you need to use the c_str member function:
:

string filename = string_convert ( x );
file.open ( (filename + ".txt").c_str() );

string_convert would be something along these lines:
:

template <typename T>
string string_convert ( T val )
{
  stringstream ss;

  ss<< val;

  return ss.str();
}


v0id Aug 16th, 2006 1:15 PM

You've to make your integer before you can use it with a string. And .open() only take C-string, so you've to use the .c_str().
:

#include <iostream>
#include <fstream>
#include <sstream>

std::string IntToString(int intValue)
{
    std::stringstream SS;
    SS << intValue;
    return SS.str();
}   

int main()
{
    std::fstream File;
    std::string myString;
   
    for(int x = 1; x <= 5; x++)
    {
        myString = IntToString(x);
        File.open((myString + ".txt").c_str());
        if(File.is_open())
        {
            // Chris?
            File.close();
        }   
        else
            std::cout << "Couldn't open the file " << (myString + ".txt") << std::endl;
    }   
   
    return 0;
}


Edit:
Damn, slow again. Very slow! :o

ReggaetonKing Aug 16th, 2006 1:37 PM

Is my way incorrect or are those different ways to do it?

Jimbo Aug 16th, 2006 2:25 PM

Quote:

Originally Posted by reggaeton_king
Is my way incorrect or are those different ways to do it?

Your method has the same fault in creating the filename.

DaWei Aug 16th, 2006 2:34 PM

Please point out the operator overloading in post #2. Your code would appear to be trying to use the insertion operator as an extraction operator, but I don't see where that's accomplished or why a love for overloading would lead one to do such a thing.

ReggaetonKing Aug 16th, 2006 3:29 PM

When using the ">>" operator with iostreams, it breaks up the white-spaced words in the file. I made a mistake instead of using ">>" I used "<<". So I have it where he could find the word "chris" in each file with those 2 loops.

So this is what I meant to do
:

ifstream in("text.txt");
string word;
while(in >> word)
{
    //compare strings
}


DaWei Aug 16th, 2006 3:43 PM

I think you also failed to take into consideration the fact that C/C++ is more strongly typed than other languages you might be familiar with. One doesn't concatenate a numeric value to a textual entity. I doubt that's a typo. Regardless, I'd suggest that you might validate your code, if you can find the time. The OP's time, wasted in being mislead, is no doubt less valuable, but still....

ReggaetonKing Aug 16th, 2006 3:46 PM

Quote:

Originally Posted by DaWei
I think you also failed to take into consideration the fact that C/C++ is more strongly typed than other languages you might be familiar with. One doesn't concatenate a numeric value to a textual entity. I doubt that's a typo. Regardless, I'd suggest that you might validate your code, if you can find the time. The OP's time, wasted in being mislead, is no doubt less valuable, but still....

Thank you Dawei, I have read the other posts sayin that you can't use x where a string is required. All the OP needs to do is convert x to a string, which other member have demostrated and then compare each word in the files using the method I provided.

I even learnt something in this thread. :)


All times are GMT -5. The time now is 12:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC