1

I'm writing a program using Xcode for school that requires we use the open() system call. I do
int input_file_desc = open(input_path, O_RDONLY);
printf("input file desc %d:\n", input_file_desc);

and it comes up with a -1. The file's path is ~/data_to_read. I set up the command line arguments in xcode. input_path is a const char * that i get from the command line. For some reason it works fine if I change the filename and command line argument to ~/data_to_read.txt. Let me know if more info is needed. thanks.

EDIT: I only tried it with .txt to see if that was the problem, but I still don't know why it needs an extension in the first place. You can have files without an extension right? In which case it should still work, as long as neither the file path nor the argument has an extension, right?

1
  • 2
    The extension is part of the name. The file name is "data_to_read.txt", not "data_to_read". Commented Jan 22, 2012 at 10:06

4 Answers 4

2

It looks like you are using C, why the C++ tag then? is it allowed? there are easy and good classes in C++ to manipulate files.

However you definitely need to include the extension in the file name (the input_path) , as many files with the same name and different extensions can exist in the same directory, so which one should be opened?

EDIT: it should be known that file extensions are (especially in UNIX-Like OS) only a "helping" thing, they are not really essential. For Example, you could have a file that contains a C++ code but has no .cpp extension, for example its name is foo only with no extensions (or even has a crazy extension like foo.bar). Still you could use the g++ to compile it, because the extension is not really important as long as the content is valid for the application that uses that file.

Sign up to request clarification or add additional context in comments.

4 Comments

It's a c++ file. i tried it with xcode and g++ in terminal, neither worked. that's why the c++ tag. but i also thought that open() was a c command. hence the c tag.
well for some odd reason when i checked the data_to_read file in terminal, it said it did have the .txt. Not sure why. But it definitely didn't have it before i added/removed it.
You mean the file didn't exist and the terminal said it does? I'm not really into Linux, however it should be known that the extension is not really an "extension", it is part of the name, as you already mentioned a file could have no extension, and there is no problem with that because the extension is nothing "real".
well it didn't have an extension when i downloaded it off the school server, then when i tested my program with it and it didn't work, i changed it to .txt, then it worked, and i changed it back to no .txt, so in Finder it said "data_to_read" and in Terminal it said "data_to_read.txt".
1

The way I understand your question, you're asking if a file needs to have an extension to be opened by the C open function, right? The answer to that is no. C does no magic for you, and will attempt to open a file with the exact filename you have specified. If the file has an extension, you must specify it in the api, if not you should not.

Check the error code returned in the errno variable (use strerror or perror to get a human readable error message) to find out what is wrong. That should point you in the right direction.

Comments

1

As other said the extension is simply part of the name.

The problem you are experiencing is probably because your OS is windows and the guys at microsoft had this dead stupid idea of "hiding" the extensions by default so your file seems to be named "test" while indeed it's named "test.txt" because it was created with notepad.

On windows systems is also common to see files named "foo.txt.txt" because this totally dumb idea of hiding/showing/guessing/automatically-adding extensions doesn't work well at all.

You can set the preferences on your computer so that file extension is always shown and this is the best thing to do on a windows system. Even better if you are interested in programming you may consider to switch for programming to an environment like linux that is less hostile to programmers.

3 Comments

I agree with your arguments about stupid hiding of file extensions. But does it apply to the OP? He isn't on Windows. In fact I just see he tells us its the mac.
@ScrollerBlaster: Sorry, you are 100% right... I missed s/he was using xcode. On my mac mini (snow leopard) I can see all file extensions even if, curiously enough, in the finder options there is a "show all filename extensions" checkbox and it's NOT checked.
@6502 yea i noticed that too, but when you check "show all filename extensions", the only new extensions i saw being shown were the .app extensions. i'm assuming that's why it's "show ALL filename extensions" instead of just "show filename extensions". the odd thing was that it was showing up without the extension in finder, and with it in terminal. but i did have the show filename extensions off at the time that i deleted the ".txt" from the filename, so maybe it just hid it instead of deleting it?
0

A filename doesn't need an extension -- a file can be called "data_to_read". But most filenames on your system do have extensions, it's just that Windows Explorer hides them from you. To open a file from a program, you need to specify this extension.

Right-click on a file in Windows Explorer and select "Properties" to find out whether a filename has an extension. Or look at the files in a Windows Command Prompt console.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.