Home > database >  Difference in using file path and file name in file-handling C
Difference in using file path and file name in file-handling C

Time:04-05

I'm currently learning file-handling, and I found out that some sources mention certain syntaxes differently. Some state fopen() as fopen("filename.type","mode"), while others mention fopen("filepath","mode") (In this case, I was searching on how to open a basic .txt file). Why does this differ? Aren't both trying to point to the same file located somewhere?

However, when I tried fopen("filename.type","mode") syntax to open a common .txt file, it's working, while fopen("filepath","mode") syntax gave me an error instead (file failed in opening). Cases are different that now I am working with a .csv file, fopen("filepath","mode") syntax works while the other one does not. Is this because .csv type is not specified in the C compiler itself? I do acknowledge that there are 2 types of files, which are .txt and .bin.

CodePudding user response:

The string in the first argument has to match the file name exactly. It is simply a label which indicates to the file system the name of the requested file.

The terminology in the documentation is not entirely stable; both the phrases you are asking about basically mean the same thing. The path terminology is strictly speaking more correct, and emphasizes that the string may contain a relative or absolute directory path as well as the file name within that directory.

C does not have any concept of a "file extension" which however is useful on some platforms to identify the file's "type". For example, Windows uses the file extension to identify (in rough terms) which application a file belongs to. But as far as C is concerned, if the file name includes an extension, that is a mandatory part of the name, and needs to be included.

(Technically, the OS could decide on your behalf which file to open if you omit the extension, but this is not how modern systems work. But for example, VMS had the concept of a file version, which was optional; if you omitted this part of the file name, the OS would always open the newest version of the file.)

If you want to open the file "/path/to/data.csv" then that is a valid file path. If your current working directory is /path/to then you can simply omit the directory, and open "data.csv" directly. You can also specify a relative path like "./data.csv" which simply uses the . alias for the current directory.

There isn't really a dichotomy between .txt and .bin files based on the extension, though some systems make a distinction between "text" and "binary" files on another level. In very brief, binary files can contain arbitrary byte streams, whereas text files have some conventions and (on some legacy systems) restrictions on what they can contain. These days, the distinction mainly pertains to normalization of line endings, where different systems still have different conventions for how to terminate a line of text; Windows uses CRLF, while Unix-based systems use plain LF. The identification of a byte stream as "text" offers some guidance for how to treat such differences.

  • Related