tags:

views:

281

answers:

1

Hi guys. I gotta problem, it's just a lil one. I have like three assignments due tomorrow night but in order to do two of them, I have to be able to access a test.txt document from my IDE. To do that, the file has to be hard coded to my C drive. I have no idea how to do this. Can somebody please help me with this? I would really appreciate it. Thank you so much.

LilProblems

+9  A: 

"hard coding" means putting something into your source code. If you are not hard coding, then you do something like prompting the user for the data, or allow the user to put the data on the command line, or something like that.

So, to hard code the location of the file as being on the C: drive, you would just put the pathname of the file all together in your source code.

Here is an example.

int main()
{
    const char *filename = "C:\\myfile.txt";

    printf("Filename is: %s\n", filename);
}

The file name is "hard coded" as: C:\myfile.txt

The reason the backslash is doubled is because backslashes are special in C strings.

steveha
Thank you so much. You are a life saver.
LilProblems
You're welcome. Good luck with your homework!
steveha
BTW, many file systems also accept the forward slash, '/', as a directory separator. The primary advantage is that you don't run into using one backslash which may get treated as the escape character. Example: "C:\temp" versus "C:/temp"; where \t is the table character.
Thomas Matthews