2
Contributors
8
Replies
14
Views
4 Years
Discussion Span
Last Post by anumash
0

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.

0

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??

Edited by anumash

0

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.

Edited by deceptikon

0

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 :)

Edited by anumash

0

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.

0

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.

0

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.