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; well i still have a problem (dont get why it will compile perfectly) but I thought i should start up ...

  1. #16
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    well i still have a problem (dont get why it will compile perfectly)
    but I thought i should start up my old thread again instead of making a new one so you can see previous problems.

    Ifstream b_file will return the right info

    Code:
    ifstream b_file ( "input1.txt" );
    but when i change all my code to use c_file it doesnt

    Code:
    ifstream c_file;
    string num = "1";
    string filename = "input";
    //skip a few lines here to this
    //while loop starts
    string filename = "input"; //
    filename += num + ".txt";
    c_file.open( filename.c_str() );
    and to debug i put this in there

    Code:
    cout<<filename;
    and it returns input1.txt

    I need c_file to increment so it has to work but i cant get it to work without incrementing

    and by the way i dont need help with the incrementing i now have
    a working code for incrementing(that I'm not using yet)


    I can show you the whole code but its 422 lines of probably overly complicated junk

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    This works for me:
    Code:
    ifstream bfile("C:\\TestData\\data.txt");
    if(!bfile)
    {
    	cout<<endl<<"Failed to open file"<<endl;
    	return 1;
    }
    int input = 0;
    bfile>>input;
    cout<<input<<endl;
    
    ifstream cfile("C:\\TestData\\data.txt");
    if(!cfile)
    {
    	cout<<endl<<"Failed to open file"<<endl;
    	return 1;
    }
    
    cfile>>input;
    cout<<input<<endl;

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Here's a more relevant version:

    Code:
    string filename = "C:\\TestData\\input"; 
    string num = "1";
    
    filename += num + ".txt";
    
    ifstream bfile;
    bfile.open( filename.c_str() );
    if(!bfile)
    {
    	cout<<endl<<"Failed to open file"<<endl;
    	return 1;
    }
    
    int input = 0;
    bfile>>input;
    cout<<input<<endl;
    
    ifstream cfile;
    cfile.open(filename.c_str());
    if(!cfile)
    {
    	cout<<endl<<"Failed to open file"<<endl;
    	return 1;
    }
    
    cfile>>input;
    cout<<input<<endl;

  4. #19
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    im going to edit my code a little bit to match that somewhat ill be back soon
    King of the overly complicated code

  5. #20
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    yes but the problem is i need to be able to increment the name of the file like input1.txt, then input2.txt
    That was already explained in previous posts. Additionally, you said:
    and by the way i dont need help with the incrementing
    Last edited by 7stud; 10-19-2005 at 03:18 PM.

  6. #21
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    yeah sorry i started to post before you finished oh and i put the failed to open code in and it says failed to open c_file so there inlies my problem i am going to look into why it cant

    edit: and does that code works for you above? the one using the string filename. cuase i am going to see the difference between myy code and yours


    and sorry for all the confusion
    King of the overly complicated code

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    If you're using a loop or opening more than one file with the same ifstream variable (like c_file), make sure you close the previous file (close()) and clear any error states (clear()) before opening the next one.

  8. #23
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    yes i am closing it now (where were you 20 minutes ago)
    it loads now and can be read but my code still has problems.
    however i think i can fix them myself for right now thanks everybody
    King of the overly complicated code

  9. #24
    Registered User sswaters's Avatar
    Join Date
    Oct 2005
    Posts
    18
    well the problem is my while loop seems to only open the c_file junk the first time around and i have no idea why and quit is equal to 0.

    Code:
    while ( quit!= 1 ) {
      filename = "input" + num + ".txt";
      c_file.open(filename.c_str()); //seems to only run once
      if(!c_file) //seems to only run once
    {
    	cout<<endl<<"Failed to open file"<<endl;
    }
    
    //code for editing data is here
    
    c_file.close();
    c_file.clear(); //no change with or without
    //if i put cout quit here i get 000000000000000 endlessly
    }//orignal while ends here
    edit: i see the problem now everytime i close the file it starts at the begginning again i can fix it
    Last edited by sswaters; 10-19-2005 at 03:59 PM.
    King of the overly complicated code

Page 2 of 2 FirstFirst 12
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