2013-12-10 76 views
0

我不知道,如果它的相关性,但我使用的是Windows 7和Microsoft Visual Studio 2010c。如果文件存在,如果是,改变输出

所有我想做的事情,就是要求用户输入文件名,检查是否存在该名称的文件存在,但它确实存在,请他们为不同的一个

我尝试

Main.cpp的

std::cout << std::endl << "You must create a username" << std::endl; 
std::cin >> username; 
user.checkFile(username); 

User.cpp

void User::checkFile(std::string &username) 
{ 

std::ifstream fin (username + ".txt"); 
    while (fin.good) 
    { 
    std::cout << "This username already exists, please choose another."; 
    std::cin >> username; 
    if (fin.bad) 
     { 
      break; 
      fin.close(); 
     } 
    } 
} 

这是否存在该名称的文件存在,但后来甚至当我键入一个不存在的名字,但它仍然告诉我正确地识别它确实存在

+1

首先, “好”和“坏”是功能。其次,你通常可以检查'fin'。最后,它并没有处于不良状态,因为如果它存在,该文件只是打开阅读。如果不是,则创建并打开它。 – chris

+0

加上break后的调用fin.close()是毫无意义的 - 这段代码永远不会到达 –

+0

@chris:IIRC,'std :: ios_base :: in'将* not *在'std :: ifstream'上创建一个文件不存在。 –

回答

1

Main.cpp的

std::cout << std::endl << "You must create a username" << std::endl; 
do 
{ 
    std::cin >> username; 
    if (user.checkFile(username)) 
     break; 
    std::cout << "This username already exists, please choose another." << std::endl; 
} 
while (true); 

User.cpp

bool User::checkFile(const std::string &username) 
{ 
    FILE *fin = fopen((username + ".txt").c_str(), "r"); 
    if (fin) 
    { 
     fclose(fin); 
     return false; 
    } 
    return true; 
} 
-1

我认为你需要使用c_str()函数来转换你的字符串。例如:

string a="df"; 
a.c_str(); // will return c string 
+0

在用户最可能使用的C++ 11中不需要。 – 0x499602D2

1

我会使用简单的C函数进行文件处理。

void User::checkFile(std::string &username) 
{ 
    std::string username2 = username + ".txt"; 
    FILE * f = fopen(username2.c_str(), "r"); 
    while (f != NULL) 
    { 
     fclose(f); 

     std::cout << "This username already exists, please choose another."; 
     std::cin >> username; 
     std::string username2 = username + ".txt"; 
     f = fopen(username2.c_str(), "r");   
    } 
} 

这样,YOUT变量username将持有有效的名称,在函数返回CAL,因为您是通过引用传递它。

+0

辉煌,工作正常(在我关闭了函数的第二行的*和f之间的差距) – user3001499

+0

叶..对不起,我错过了分号 –

0

输入新的字符串后,您需要重新打开该文件与该名:

std::cout << "This username already exists, please choose another."; 
std::cin >> username; 

fin.close(); 
fin.open(username); 

此外,goodbad是必须使用电话运营商()中调用的函数。

1
int main(void) { 

    bool fileExists = true; 

    while (fileExists) 
    { 
      string fileName; 
      cout << "Enter file name: "; 
      cin >> fileName; 

      ifstream ifs(fileName.c_str()); 

      fileExists = !ifs.good(); 
    } 

    return 0; 
} 
+0

我会使用'do/while'循环来代替。不需要'fileExists'变量。 –

+0

@RemyLebeau:你仍然需要在循环之外的变量来测试条件。如果你将'ifs'移出循环以检查它是否处于终止状态,那么你需要做额外的工作来正确地打开/关闭它。哪个不理想。使用'filesExists'外部(作为一个简单的布尔)while循环很好很容易。 –

+0

@LokiAstari:不,你不需要一个外部变量。如果找不到该文件,请输入“do”和“break”循环。 'do {ifstream ifs(filename); if(!ifs)break; } while(true);' –

1

如果你限制自己只是 Windows中,有一个API函数正是这么做的:PathFileExists

如果你想坚持的标准库,你可以做到以下几点:

string filename; 
cin >> filename; 
ifstream fin(filename); 
if (fin.is_open()) 
{ 
    // file opened successfully 
} 
else 
{ 
    // file did not open 
} 
+1

对于更接近标准的东西,Boost的文件系统库有它。如果使用MSVC,即将成为标准的''头已经在那里。 – chris

+0

@chris:的确,我差点忘了那个图书馆:) –

相关问题