I was wondering how the compiler searches for a file using just the name of the particular file. The function argument that should be supplied is the file name in double quotes and mode i.e. read, write etc. What if there are two files of the same name but in different directories? Which file does the compiler load in the memory? Suppose if I want to open a binary file in which directory should I place it for it to successfully load in memory?

If I have loaded a binary file how am I supposed to know that at what position the file is ended. For text files the ASCII value of 26 signified end of file, how do we figure out the end point of a binary file?

Recommended Answers

All 8 Replies

I was wondering how the compiler searches for a file using just the name of the particular file.

Typically, a relative path will behave as if the current working directory were prepended. For example, on Windows "file.txt" would be treated as if it were "%cd%\file.txt".

If I have loaded a binary file how am I supposed to know that at what position the file is ended. For text files the ASCII value of 26 signified end of file, how do we figure out the end point of a binary file?

In all cases (text and binary) the standard library will tell you when end of file was reached. This usually means the error return code for the library input function, and feof() can be used to differentiate between end of file and an actual stream error.

How do I know what is my current directory and how should I change it? Also, what do you mean by "error return code for the library input function". Does that mean the macro EOF??

How do I know what is my current directory and how should I change it?

What compiler and OS are you using? There's not a standard solution, but there are a few fairly consistent rules:

  1. When running from the command line, whatever directory you're in at the point of program invokation is the current working directory. Note that you can call a program from another directory by specifying its full or relative path.

  2. When running from an IDE, the current working directory is either the location of the executable file in your build directories or a nested debug directory.

  3. When running from a GUI (ie. clicking on its icon to run a program), the current working directory could be either the directory where the executable file resides or a configured default such as the user's home directory or documents folder.

It's best to ensure that you're in the directory you want, but as mentioned there's no standard way to do that. However, most compilers will support some form of the getcwd() and chdir() functions that retrieve and set the current working directory.

Also, what do you mean by "error return code for the library input function". Does that mean the macro EOF??

The EOF macro is included, but not the only thing I meant. fgets() returns NULL as a failure code, not EOF, so you need to account for variances like that and also check feof() if there's a possibility that the error could be unrelated to reaching end of file.

Thanks for your reply deceptikon!! I am using Dev C++ as an IDE and the compiler is MinGW. OS is win 7. On using the cmd I get a path in the C directory. How do I find out the path. Also, is there any way to read integers or float from a file like wav file. fgetc() accomplished that for reading characters. Sorry for asking so many questions :)

How do I find out the path.

Google for getcwd() and chdir().

Also, is there any way to read integers or float from a file like wav file.

Yes, but if you're reading a binary file with a specific format you'll need to conform to that format when reading the file. Look up the specification for the format here. Often it's not as simple as just reading a float or any integer, you need to fully parse the file.

I have a germ of an idea as to how my program should work. I first determine the directory using getcwd() and copy the wave file into that directory then I write the program and using the file structure open the file using fopen(aa.wav, rb) and then read the header of the wav file i.e the RIFF specification. I cannot download the file format for .wav from the link you just posted but I checked on other sites and I found the .wav format. The thing is that I have seen the samples using MATLAB and I know the samples are unsigned 8 bit samples (centered at 127) and the sampling frequency is of 16khz. Hence I just want the unsigned samples as an array. Any suggestions as to how to proceed further? The following site says that the data chunk starts from the 44th byte. https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ Also I know my samples are integers occupying 1 byte. I am currently reading a C text book which has chapters dedicated to file I/O but there is no mention of reading integers from a binary file.

I am currently reading a C text book which has chapters dedicated to file I/O but there is no mention of reading integers from a binary file.

Look up the fread() function for a simple start.

Hey thanks a lot for your reply!! Gave a much needed breakthrough!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.