Hi,

I'm writing a code that takes a file name from user and opens it if it exist and if it doesn't asks for another file name. What i wrote is this:

#include <iostream>
#include <string.h>
#include <string>
#include <stdlib.h>

using namespace std;

int mani()
{
  string input;
  cout<<"please enter the file name:"<<'\n';
  getline(cin, input);
  FILE *log =fopen (input,"r")
  if (!log)
    printf("can not open the file.\n");
  else
    break;
  // fclose (log);                                                              
  return 0;
}

But I'm getting these errors. Any suggestion what i need to do?

ReadFileName.cpp:13: error: cannot convert 'std::string' to 'const char*' for argument '1' to 'FILE* fopen(const char*, const char*)'
ReadFileName.cpp:14: error: expected ',' or ';' before 'if'
ReadFileName.cpp:16: error: expected primary-expression before 'else'
ReadFileName.cpp:16: error: expected `;' before 'else'

Edited 6 Years Ago by YasaminKh: n/a

For the first error, use input.c_str(), which makes it the correct parameter type.

For the second error, add ; at line 13. Forgot that =P

For the next two errors, uncommon fclose(); You can't keep a file running open.

Thanks a lot for your help. It seems that it works but could you please explain it a little bit for me. I'm not familiar with input.c_str() type.

I have another question. When user enters an invalid File name, i expect the program to print: "it's not a valid name " and ask for a new name. But I tried it with an invalid file name and i got: Segmentation fault
Could you explain this for me as well? and how can I fix it.

I really appreciate your time and help.
Here is my new code, I made some minor changes in it:

#include <iostream>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <cstdlib>

using namespace std;

int main()
{
  string input;
  //  input.c_str();                                                                        
  while (1)
    {
  cout<<"please enter the file name:"<<'\n';
  getline(cin,input );

  FILE *log =fopen (input.c_str(),"r");
  if (!log)
    printf("it's not a valid name");
  else
    break;
  fclose (log);
}
  return 0;
}

string.c_str() returns the string as a char* string. Which is the required type.
As for closing, the code should be:

if(!log)
 .
else
{
 fclose(log);
 break;
}

Put that in a while loop to prompt user to enter file till correct.
Seg fault is probably as your trying to close a handle which infact does not exist.

nbaztec thank a lot for your help. my code works now. :)

Edited 6 Years Ago by YasaminKh: n/a

I'm sorry.
Just one last question. Now i want to put this in another code that i wrote before.
I need the file, that user enters in this program to be opened in the bigger program. How can I do that? I think something like return might help but I'm not sure if return could come with a non integer value.

>>return could come with a non integer value.
You can return any valid datatype or a reference to it.

This question has already been answered. Start a new discussion instead.