a little if of stream help please

This is a discussion on a little if of stream help please within the C++ Programming forums, part of the General Programming Boards category; hello please answer quick as you can it is 1:35 here and not pm I am writing a program that ...

  1. #1
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18

    a little if of stream help please

    hello please answer quick as you can it is 1:35 here and not pm

    I am writing a program that needs to input files but the name of the file needs to increment everytime a while loop runs. I can handle the while loop but i cant seem to get any type of variable in the ifstream command.
    I want for everytime the while loop runs for this to happen
    Code:
    THIS CODE IS FOR EXPLANING ONLY IT DOESNT RUN DONT TRY TO COMPILE
    ifstream b_file ( "input1.txt" );
    while loop runs
    ifstream b_file ( "input2.txt" );
    while loop runs
    ifstream b_file ( "input3.txt" );
    etc.
    If you know how to do this please respond here or if you need more info


    If i have broken any rule please forgive me and my eyes cuase its 2 in the morning and i cant see anything

    and
    this is only the beggining of my problems so if someone can be ready to help anytime I need
    please email me please

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, you're creating the b_file variable too many times... your code should look like this:
    Code:
    ...
    ifstream b_file;
    
    --enter while loop--
    
    b_file.open("input1.txt");
    do something
    b_file.close();
    increment file name
    b_file.open("input2.txt");
    do something
    b_file.close();
    increment file name
    
    --exit while loop--
    ...
    I'm assuming you know how to increment the filename because you said you had the while loop down.
    Last edited by major_small; 10-18-2005 at 01:25 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    I think you misunderstood what i was trying(poorly trying) to say but u answered parts of what i was asking so thanks

    but what i am trying to do would be like this
    Code:
    ifstream b_file;
    
    --enter while loop 1st time--
    b_file.open("input1.txt");
    do something
    b_file.close();
    increment file name
    --exit while loop--
    
    --enter while loop 2nd time--
    b_file.open("input2.txt");
    do something
    b_file.close();
    increment file name
    --exit while loop--
    and i do not know how to increment the filename(if you can tell me i will be very gratefull) for this although i can add 1 to an integer/a letter to a char every time the while runs and things like that

    and if you want the whole code of this program to understand what i am talking about I can give it to you but it is 255 lines long and has a bunch of wierd programmin in it

    edit: and this is the problem i know whats wrong in, the others are like why the hell does it do that.

    Once it didnt output two lines untill i changed something that was completly(well obviously somehow it is) not connected to the output.
    Last edited by sswaters; 10-18-2005 at 01:42 AM.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well... here's a bit of code to help you in the right direction... I'm sure there's a more efficient way of doing it though:
    Code:
    #include<iostream>	//for standard console I/O
    
    int main()	//main entrypoint
    {
    	char filename[]="input00.txt";	//declare the filename
    	
    	try //for error handling
    	{
    		for(register short int i=0;i<200;i++)	//this is your looping structure
    		{
    			if(i<10)	//if the number's less than 10
    			{
    				filename[6]=static_cast<char>(i+'0');	//change the number in the one's column
    			}
    			else if(i<100)	//if the number's greater than 10 (it must be less than 100)
    			{
    				filename[5]=static_cast<char>(static_cast<int>(i/10.0f)+'0');	//put the number in the tens column in it's place
    				filename[6]=static_cast<char>((i-(static_cast<int>(i/10.0f)*10))+'0');	//extract the number in the one's colum
    			}										//and put it where it goes		
    			else //if there's more than 99
    			{
    				char e[]="Error - File Numbers Have Exceeded Maximum (99 allowed)";	//create an error message
    				throw(e);	//throw the message
    			}
    		
    			std::cout<<filename<<std::endl;	//output the filename (here you'd open the file and do whatever you need)
    		}
    	}
    	catch(char*e)	//catch any errors that match a char*
    	{
    		std::cerr<<e<<std::endl;	//output the pointer and a newline
    	}					//do whatever you want to try to recover.
    
    	return 0;
    }
    edit: you should be able to use the filename variable inside the open() method of the stream you're trying to use.

    edit2: that code should compile, and I set it up to fail.
    Last edited by major_small; 10-18-2005 at 02:08 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    thanks i will see if it works once i make small adjustments to my code and maybe that code

    and by the way efficient doesnt really metter to me just look at this snippet of my (poorley written but working) code:
    Code:
    b_file>> cl;
    b_file>> cl;
    (cl is CurrentLetter) and that repeats unfortunatley 31 more times to skip 30 characters(if you know a way to ignore characters please tell me but i was to lazy to look it up after trying to look it up for 10 mins without success

    edit: and by the way did you just write that code if so you program 1000 times faster than anybody i know
    Last edited by sswaters; 10-18-2005 at 02:16 AM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    The crux of the problem is being able to convert an int to a string. A for-loop has a counter that's an int, and if you can convert it to a string, then you can append it to your filename, like this:

    Code:
    string filename = "input" 
    string num = "1";
    filename += num + ".txt";
    To convert an int to a string, you can use what's called a stringstream. You use a stringstream like you were writing to a file--only you write data to a string variable instead. Like when you write to a file, you can write various data types to a stringstream. Here is an example:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
    	string filename = "input";
    	
    	for(int i=0; i<3; i++)
    	{
    		ostringstream outputString; //output stringstream--like an output file 
    		outputString<<i;  //the arrows indicate which way the data is flowing, 
    			         //i.e. data is flowing into the variable outputString
    		filename += outputString.str() + ".txt";
    		cout<<filename<<endl;
    
    		filename = "input";
    	}
    
    	return 0;
    }
    You can use filename.c_str() to create your filestream. The c_str() member of the string class returns a c-style string version. I couldn't figure out how to an erase a stringstream, so the code just recreates a new stringstream everytime through the loop.
    Last edited by 7stud; 10-18-2005 at 03:07 AM.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by sswaters
    if you know a way to ignore characters please tell me but i was to lazy to look it up after trying to look it up for 10 mins without success
    b_file.ignore(30);
    Quote Originally Posted by sswaters
    edit: and by the way did you just write that code if so you program 1000 times faster than anybody i know
    heh, yeah, and thanks... like 7stud said though, using the string class may be more helpful than my method, because then you don't have to worry about limiting the amount of files... you could just do something like:
    Code:
    empty the string
    start the string off with "input"
    add the number (look into itoa())
    add ".txt"
    that may be more simplistic.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    itoa(), etc. aren't standard C++, so if you use those beware.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    oh yes... I forgot to add that note... thanks 7stud
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    well thanks for all the help just refreshed to find that ignore char thing that will make things easier thanks all of you i think thats all i need

    edit:nowhere else would i find such help at 1-3 in the morning here
    Last edited by sswaters; 10-18-2005 at 03:36 AM.

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    itoa(), etc. aren't standard C++, so if you use those beware.
    stringstreams are and they are great .
    Code:
    #include <iostream>
    #include <ostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
        std::string someString;
        std::stringstream sstream;
        sstream<<"Input" << 1 <<".txt";
        someString = sstream.str();
        
        std::cout<<someString<<std::endl;
        
        std::cin.get();
    }
    Woop?

  12. #12
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    b_file.open(filename);
    gets errors

    Code:
    50 no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::open(std::string&)'
    Code:
    C:\Dev-Cpp\include\c++\3.4.2\fstream:695 candidates are: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
    I probably screwed up

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    You will want to use filename.c_str(). For some reason File streams don't accept strings as arguments only char *.
    Woop?

  14. #14
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    will try lol one sec

    omg it worked finally what a silly thing it doesnt accept it like that but will as filename.c_str()

    you guys have saved me weeks of researching on google

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I probably screwed up
    From above:
    Quote Originally Posted by 7stud
    You can use filename.c_str() to create your filestream. The c_str() member of the string class returns a c-style string version.
    omg it worked finally what a silly thing it doesnt accept it like that but will as filename.c_str()
    All functions are defined to take specific types as arguments. If you don't provide the type the function is looking for, you will get an error. Filestreams require a c-style string which is a character array, not a string type.
    Last edited by 7stud; 10-18-2005 at 01:26 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New stream flushing FAQ candidate?
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 07-01-2009, 05:57 PM
  2. Discard wrong data from stream
    By mesmer in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 01:30 AM
  3. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 04:15 PM
  4. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  5. Replies: 9
    Last Post: 07-01-2002, 07:50 AM

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