When you open a file, all kinds of things can go wrong. A file lives on a physical device — a fixed disk, for example, or perhaps on a flash drive or SD card — and you can run into problems when working with physical devices.
Oct 08, 2016 What do you see is the problem? Ppl say that it compiles well, but for me it is not the case. Thank you for your help. I'm trying to set windows programming enviroment for c. I use the Visual Studio to write the code but my University wants me to use g compiler. Fatal error: no input files compilation terminated. But if i use: g hello.cc. What greyfade said, and furthermore if you try to compile a.c file with g it will compile it as C.
For example, part of the disk might be damaged, causing an existing file to become corrupted. Or, less disastrous, you might run out of disk space. Or, even less disastrous, you might try to open a file in a directory that doesn’t exist.
If you try to open a file for writing by specifying a full path and filename but the directory does not exist, the computer responds differently, depending on the operating system you’re using. If you’re unsure how your particular operating system will respond, try writing a simple test application that tries to create and open something like /abc/def/ghi/jkl/abc.txt. (Of course, you’ll want to be sure to use a directory that doesn’t exist.)
Then one of two things will happen: Either the directory and the file will get created, or nothing will happen.
For example, on a Windows system, if we attempt to create a file in a directory that doesn’t exist, the system does not create the directory. That’s because deep down inside, the application ultimately calls an operating system function that does the dirty work of creating the file. And this particular operating system function (it’s called CreateFile(), if you even care) has a rule that it will not create a directory for you.
If you want to determine whether the ostream class was unable to create a file, you can call its fail() member function. This function returns true if the object couldn’t create the file. And that’s what happens when a directory doesn’t exist. The DirectoryCheck01 example shown demonstrates an example of this.
When you run this code, assuming that you don’t have a directory called /abc/def/ghi on your system, you should see the message Couldn’t open the file! Assuming that your particular operating system doesn’t create a directory in this case; if it does, your computer will open the file, write Hi to it, and move on with its happy life after closing things out.
As an alternative to calling the fail() member function, you can use an operator available in various stream classes. This is !, fondly referred to as the “bang” operator, and you would use it in place of calling fail(), as in this code:
Most people prefer to use !outfile instead of outfile.fail(), although !outfile makes confusing code. The reason is that outfile is an object, and the notion of !outfile simply doesn’t make sense.
In fact, !outfile trips up many beginning programmers. They know that outfile is not a pointer in this sample code, and they wonder how you could test it against 0 as you normally can only do with a pointer. (Remember, by saying !x, where x is some pointer, you’re testing x against 0.) And that simply doesn’t make sense! And so, to avoid confusion, just call fail(). It makes more sense.
Here are some reasons your file creation may choke:
The directory doesn’t exist.
You’re out of disk space and out of luck.
Your application doesn’t have the right permissions to create a file.
The filename was invalid — that is, it contained characters the operating system doesn’t allow in a filename, such as * or ?.
Like any good application, your application should do two things:
1.Check whether a file creation succeeded.
2.If the file creation failed, handle it appropriately.
Don’t just print a horrible message like Oops!Aborting!, leaving your poor users with no choice but to toss the monitor onto the floor. Instead, do something friendlier — such as presenting a message telling them there’s a problem and suggesting that they might free more disk space.
C Programming Obtaining a compiler |
Wikipedia has related information at Dev-C++ |
Dev C++ is an Integrated Development Environment(IDE) for the C++ programming language, available from Bloodshed Software. An updated version is available at Orwell Dev-C++.
C++ is a programming language which contains within itself most of the C language, plus extensions. Most C++ compilers will compile C programs, sometimes with a few adjustments (like invoking them with a different name or command line switch). Therefore, you can use Dev C++ for C development.
However, Dev C++ is not the compiler. It is designed to use the MinGW or Cygwin versions of GCC - both of which can be obtained as part of the Dev C++ package, although they are completely different projects.
Dev C++ simply provides an editor, syntax highlighting, some facilities for the visualisation of code (like class and package browsing) and a graphical interface to the chosen compiler. Because Dev C++ analyses the error messages produced by the compiler and attempts to distinguish the line numbers from the errors themselves, the use of other compiler software is discouraged since the format of their error messages is likely to be different.
The latest version of Dev-C++ is a beta for version 5. However, it still has a significant number of bugs. All the features are there, and it is quite usable. It is considered one of the best free software C IDEs available for Windows.
A version of Dev C++ for Linux is in the pipeline. It is not quite usable yet, however. Linux users already have a wealth of IDEs available. (e.g. KDevelop and Anjuta.) Most of the graphical text editors, and other common editors such as emacs and vim, support syntax highlighting.
The GNU Compiler Collection (GCC) is a free/libre set of compilers developed by the Free Software Foundation and can be installed on a wide variety of operating systems. GCC commands are used throughout this book to demonstrate how to compile C code so you are encouraged to take the time to install GCC on your machine.
On GNU/Linux, Installing the GNU C Compiler can vary in method from distribution to distribution. (Type in cc -v to see if it is installed already.)
sudo apt install build-essential
, or by using Synaptic. You do not need Universe enabled.apt install gcc
.yum install gcc
.rpm -ivh gcc-version-release.arch.rpm
urpmi gcc
installpkg gcc-xxxxx.tgz
emerge -uav gcc
.pacman -S gcc
.The simplest method for obtaining a compiler is to install Apple's proprietary IDE, Xcode, available for free.
Xcode comes bundled with a gcc-compatible compiler called clang which replaced GCC as Xcode's default C compiler a number of years ago. But because Xcode aliases the gcc
command to the clang compiler, GCC installation isn't necessary to compile the example code in this book.
If you prefer using the GCC compiler, the third-party package manager, Homebrew, provides an easy installation process. You'll first need to install Homebrew, and then issue the brew install
command to install the desired GCC Homebrew formulae. You may want to find a recent tutorial that will step you through this process as other commands may be necessary to get GCC set up flawlessly on your system, especially if you already have Xcode installed.
For hardcore computer enthusiasts, GCC can be compiled directly from the source code. We highly recommend searching out and following an up-to-date tutorial for installing GCC from source files.
There are two ways to use GCC on Windows: Cygwin and MinGW. Applications compiled with Cygwin will not run on any computer without Cygwin, so MinGW is recommended. MinGW is simpler to install, and takes less disk space.
To get MinGW, do this:
To get Cygwin, do this:
Third option is to use WSL:
The current stable (usable) version of GCC is 4.9.1 published on 2014-07-16, which supports several platforms. In fact, GCC is not only a C compiler, but a family of compilers for several languages, such as C++, Ada, Java, and Fortran.
We have a long list of C compilers in a much later section of this Wikibook.Which of those compilers would be suitable for beginning C programmers, that we should say a few words about getting started with that particular compiler in this section of this Wikibook?
C Programming Obtaining a compiler |