My assignment asks me to access a test.txt
document, so the file name has to be hard coded to my C drive. I have no idea what hardcoding means. Can somebody please help me with this?

- 5,099
- 4
- 53
- 73

- 657
- 1
- 8
- 11
4 Answers
"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.

- 52,325
- 13
- 128
- 140

- 74,789
- 21
- 92
- 117
-
7BTW, 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 Dec 14 '09 at 18:41
-
8Windows supports '/' just fine, but other filesystems in wide use do not support '\'. For this reason, I always use '/' as my path separator. – David Stone Jun 09 '12 at 01:55
"Hard Coding" means something that you want to embed with your program or any project that can not be changed directly.
For example, if you are using a database server, and hard code to connect your database with your project, then that can not be changed by the user.

- 23,933
- 14
- 88
- 109

- 171
- 1
- 2
There are two types of coding.
(1) hard-coding
(2) soft-coding
Hard-coding. Assign values to program during writing source code and make executable file of program. Now, it is very difficult process to change or modify the program source code values. Like in block-chain technology, genesis block is hard-code that cannot changed or modified.
Soft-coding: it is process of inserting values from external source into computer program. Like insert values through keyboard, command line interface. Soft-coding considered as good programming practice because developers can easily modify programs.

- 3,658
- 3
- 31
- 40

- 358
- 3
- 9
Scenario
In a college there are many students doing different courses, and after an examination we have to prepare a marks card showing grade. I can calculate grade two ways
1. I can write some code like this
if(totalMark <= 100 && totalMark > 90) { grade = "A+"; }
else if(totalMark <= 90 && totalMark > 80) { grade = "A"; }
else if(totalMark <= 80 && totalMark > 70) { grade = "B"; }
else if(totalMark <= 70 && totalMark > 60) { grade = "C"; }
2. You can ask user to enter grade definition some where and save that data
Something like storing into a database table
In the first case the grade is common for all the courses and if the rule changes the code needs to be changed. But for second case we are giving user the provision to enter grade based on their requirement. So the code will be not be changed when the grade rules changes.
That's the important thing when you give more provision for users to define business logic. The first case is nothing but Hard Coding.
So in your question if you ask the user to enter the path of the file at the start, then you can remove the hard coded path in your code.

- 2,167
- 4
- 27
- 52