Thread: Looping code with a viable ios::noreplace code

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    4

    Looping code with a viable ios::noreplace code

    Ok this topic may make no sense at first but here's the code I'm having trouble with.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    .............................
    
    while(!savefile.is_open())
    {
    	cout << "Please give me a name." << endl;
    	getline(cin, name, '\n');
    	fstream savefile (name.c_str(), ios_base::in);   //To check if file exists
    	if(!savefile)                                                        //If file doesn't exist,  create it.
    		ofstream savefile (name.c_str())
    		 
    	else                                            //If file does exist loop back and ask for dif name
    		cout << "Name already in use. Please choose another name." << endl;
    ....................................
    }
    Ok heres what is happening with this code when compiled and ran. It is able to tell me the file exists and will ask for input of a dif file name (so getting into the loop works fine).
    The problem is when i give it a name that doesn't exist. It'll take the name and create the folder yet it'll loop back to asking for a name again rather then breaking the loop. It'll do this no matter how many times i give it a new name.

    Heres what I have tried.
    I've tried making sure the file is open with savefile.open(name.cstr()) (still loops after new name entered).
    I've tried the condition for while to be !savefile.
    I've tried a do while loop with condition being both !savefile and !savefile.is_open()
    I can't remember much else but I'm sure my problem relys in the condition and can probable be fixed with an extra in variable. Kinda like this.

    Code:
    int x = 0; //new variable
    
    while(x!=1)
    {
    	cout << "Please give me a name." << endl;
    	getline(cin, name, '\n');
    	fstream savefile (name.c_str(), ios_base::in);   //To check if file exists
    	if(!savefile)
            {                                               //If file doesn't exist,  create it.
    		ofstream savefile (name.c_str())
    	        x=1;
              }	 
    	else 
            {                                   //If file does exist loop back and ask for dif name
    		cout << "Name already in use. Please choose another name." << endl;
                    x=0;
              }
    I have thought of this fix but am tring to find a fix that does not cause unnecessary variable.
    Thanks for any help in advance.
    Last edited by Medic87; 09-06-2010 at 10:09 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The way I would do this:

    1. Save to a unique file name such as temp001 -- close the stream
    2. Ask for a save file name
    3. Rename your file with std::rename() (from cstdio)
    4. If rename should fail, the name is in use.

  3. #3
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    You could use a while(1) loop and then break out after you save the file.

    Also, are you using the same identifier name in 3 nested scopes? It looks like you've got a savefile in main, a savefile in the loop, and then another savefile in the conditional block... that's really asking for trouble, IMO.
    Last edited by BMJ; 09-06-2010 at 11:08 PM.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    Unfortunately I've haven't done much c++ lately so i've lost alot and am now trying to get it back. As of right now i have it working correctly. It is recognizing if file is there or not and now does break loop when a new file is created. As far as the code goes on there its just an example to my actual code. The problem with have to redefine savefile is that its in a switch case. I haven't found away around the skip switch case error yet.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well whatever works I guess. A switch case doesn't sound like a compelling reason to avoid simply renaming a file, though. You can always define a function and call it in the applicable cases.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    fstream is not a defined variable in my int main(). Its just included file type so I'm able to use it. The first time the fstream recieves a variable name and variable (file.txt) is at the beginning of switch(variable) case1:. Which is where the code is located i was asking about. I'm not sure if this clears up a little but i don't use savefile anywhere but here. I just couldn't compile it without it declared with in {} with in the switch case because of an error message of variable skipped in case 2: or something of that nature. So yes its been declared within the switch case as
    Code:
     fstream savefile;
    and then opened in the loop to check to see if file exists, but if the loop is within the same {} then i don't see a double nestle here. Maybe a bit more explanation on what is meant as i have it nestled in 3 areas?

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    Quote Originally Posted by Medic87 View Post
    fstream is not a defined variable in my int main(). Its just included file type so I'm able to use it. The first time the fstream recieves a variable name and variable (file.txt) is at the beginning of switch(variable) case1:. Which is where the code is located i was asking about. I'm not sure if this clears up a little but i don't use savefile anywhere but here. I just couldn't compile it without it declared with in {} with in the switch case because of an error message of variable skipped in case 2: or something of that nature. So yes its been declared within the switch case as
    Code:
     fstream savefile;
    and then opened in the loop to check to see if file exists, but if the loop is within the same {} then i don't see a double nestle here. Maybe a bit more explanation on what is meant as i have it nestled in 3 areas?
    OK, I understand more what you mean now. I have toyed with the code for just a few min and realized that all i needed to do was declare the variable fstream under func main. after this is done the switch case doesn't matter.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    // Declare variables
    int c, x, b;
    string a;
    fstream savefile;
    
    //after savefile declared here as a fstream  i can write the noreplace replacement like this
    
    switch(x)
    case1:
    savefile.open (name.c_str(), ios_base::in); //Just savefile.open instead of fstream savefile in first post
    
    if(!savefile) //If file doesn't exist,  create it.				
    {					
    savefile.open (name.c_str()); //same here
    savefile << name << endl;
    
    }
    else    //If file does loop back and ask for dif name
    {
    cout << "Name already in use. Please choose another name." << endl;
    }
    I didn't include the loop in this paste though because I already figured that out. But now my program no longer names savefile as fstream 3-4 times nestled in different brackets.

    Code:
    fstream savefile; //In main an then
    
    fstream savefile(name.c_str()); /* In switch and again in loop. Just showing i know what you mean now  and thanks for help*/
    Thx again for pointing that out to me so i'm no longer saying savefile is an fstream throughout three different brackets.
    And I also found out the reason for the switch case error is that instead of anouncing my fstream as savefile right after main before i use switch case i was trying to just announce it in case 1: which gave me the error.
    Last edited by Medic87; 09-07-2010 at 06:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  2. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  3. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  4. Help With C code looping
    By maker10 in forum C Programming
    Replies: 11
    Last Post: 10-23-2008, 10:57 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM