I open up Dev C++ 5.11 version, Click FIle => New Project => Console Application
ID: 673965 • Letter: I
Question
I open up Dev C++ 5.11 version, Click FIle => New Project => Console Application => C code => Enter. DOne in C code and COnsole Application.
You are given 10 files with random integers in them of unkown size. They are named in1.txt through in10.txt. Write one output file, called out.txt that has in order the number of points of each input file. All you need from the input files is how many points they have. The following code can help you on how to automate calling multiple filenames: #include #include #include int main) int i; char fname [1001: //just long enough for any filename size for(i-1; iExplanation / Answer
Data processing is an important application in computing. To process large amounts of data, we need some way to store and access that data. The most natural way to accomplish this is to store data in files and then provide some mechanism for reading and writing data from and to those files.
C offers some very simple mechanisms for reading and writing data from files. The simplest of these mechanisms is the mechanism for working with data stored in text files. C also offers ways to work with binary files, but since text files will suffice for our needs in this course I will only be discussing how to work with text files.
The first step in working with a file is to create a file pointer variable. This is done with a variable declaration
The FILE data type is a special data type defined in the stdio.h header file. Most functions designed to work with FILE variables will expect us to pass a pointer to that variable. The * you see in the variable declaration indicates that the variable input is a pointer to a FILE. We will discuss pointer variables in detail later in the term. For now, all you have to know is that the line shown above is the appropriate syntax for setting up a FILE pointer variable, and that will suffice for the time being.
The second step in working with a file is to open the file for reading or writing. This is done by calling the fopen function.
fopen takes two parameters. The first is the name of the file to open, and the second is a text string that specifies an open mode to use. To open a text file for reading data, we use an open mode of "r". To open a text file for writing, we will use an open mode of "w".
In specifying the name of the file to open we can use two methods. The simple method is to just name the file. For this to work correctly, the file has to be placed in a location where it can be found easily. In the case of Visual Studio, this means placing the text file in the same folder as the source code for the program. In other development environments this means placing the file in the same directory where the compiled program resides after compilation. You may have to experiment a little with your development environment to figure out where this default location is. A second, more reliable alternative for specifying a file is to give a complete path to the file. On Windows a complete path can look something like the following:
The file I/O functions in the stdio library are:
If you want to read a file you have to open it for reading in the read (r) mode. Then the fgets library functions can be used to read the contents of the file. (It is also possible to make use of the library function fscanf. But you have to be sure that the file is perfectly formatted or fscanf will not handle it correctly).
to open the file with names like in%d,
int i; for (i=0; i<1000; i++) {
static char F[] = "in%d";
char f[sizeof F+3];
sprintf(f,F,i);
FILE *fn = fopen(f,"r"); if (!fn) {perror(fn); continue;}
fclose(fn);
}