Академический Документы
Профессиональный Документы
Культура Документы
POPULAR POSTS
How to Build an iPhone App from Scratch for NonTechnical People: Your quick and dirty guide
Before you can read from a file, you must open it. And once you have finished using the file, you should close it to
allow other applications access. The two functions you use here are fopen() and fclose(). Although the focus of this
explanation is on fread(), not general file-handling functions, a brief introduction to these routines might be
helpful.
fopen() opens a file for reading or writing, and returns a pointer to a file stream. It accepts two arguments: the
first is the file name (optionally including the file path) of the file to be opened; the second is the access mode,
which can be one of six settings. The access mode used in the sample code included with this article is r, which
opens a file for reading. The file must exist when using this argument.
fclose() closes the file and releases any locks. It accepts one argument: the pointer returned by a successful call to
fopen().
compiler particularly the declaration of the main() function. You should be aware that the compiler is set to use
ANSI C89 and so local variables must be declared at the start of functions.
/*
*
* Udemy.com
* Reading from Files in C Using Fread
*
*/
#include
int main( int argc, const char* argv[] ) {
FILE *rm;
char buf[201];
printf("\r\nUdemy.com - Reading from Files in C\r\n");
rm = fopen("ReadMe.txt", "r");
if (rm != NULL) {
fread(buf, 1, 200, rm);
buf[(sizeof buf)-1] = 0;
printf("%s", buf);
fclose(rm);
}
else
printf("File Not Found.\r\n");
}
The first argument that it accepts is a pointer to an area of memory that will be hold the data read from the file.
In this example, the code specifies an array of chars (buf) as the destination.
The size argument defines the size of a single element being read. The char data type is a single byte, and so the
https://blog.udemy.com/fread-c/
1/4
16/4/2015
The size argument defines the size of a single element being read. The char data type is a single byte, and so the
value used here is 1.
nmemb specifies how many elements (of the size specified) to read from the file. A value of 200 reads up to 200
characters from the file.
Finally, the stream argument specifies which file stream to read from usually the pointer returned from a call to
fopen().
There are a few other important points to note in the code above.
1. The return value of fopen() is checked against null to ensure that the file was opened successfully.
2. The code does not check that the call to fread() was successful and whether it returned the number of
characters requested.
3. The final element in the buffer array is set to 0 to ensure that the string is null-terminated, prior to
sending the result to the console using printf().
If the file contains fewer bytes than the number requested, the call to fread() reads as many values as it can and
returns a size_t value indicating how many were read. The example below defines a buffer array of 2000
characters, and then shows how many were actually read from the file stream.
/*
*
* Udemy.com
* Reading from Files in C Using Fread
*
*/
#include
int main( int argc, const char* argv[] ) {
FILE *rm;
char buf[2000];
int res;
printf("\r\nUdemy.com - Reading from Files in C\r\n");
rm = fopen("ReadMe.txt", "r");
if (rm != NULL) {
res = fread(buf, 1, 2000, rm);
printf("Read %u bytes.\r\n", res);
fclose(rm);
}
else
printf("File Not Found.\r\n");
}
rm = fopen("ReadMe.txt", "r");
if (rm != NULL) {
while (!feof(rm)) {
res = fread(buf, 1, (sizeof buf)-1, rm);
buf[res] = 0;
printf("%s", buf);
}
fclose(rm);
}
https://blog.udemy.com/fread-c/
2/4
16/4/2015
compiler places your compiled executable. The code below reads a short (a 16-bit integer) from the text file by
specifying the element size (2) and using the address of a short variable as the destination pointer.
/*
*
* Udemy.com
* Reading from Files in C Using Fread
*
*/
#include
int main( int argc, const char* argv[] ) {
FILE *tf;
short res;
printf("\r\nUdemy.com - Reading from Files in C\r\n");
printf("Size: %u\r\n", sizeof res);
tf = fopen("Test.txt", "r");
fread(&res, sizeof res, 1, tf);
fclose(tf);
printf("Value: %u\r\n", res);
}
On Windows, the value output will be 16,961 if the first two characters in the text file were A (0x41) and B (0x42).
The two bytes read from the file overwrite the two bytes in memory used by the short, giving res the new value
0x4241 (the Intel x86 architecture is little-endian).
Overwriting the area of memory used by a data type is not limited to single variables. This final code sample reads
the same four characters from the text file, and overwrites the memory used by a struct. In this way, entire
structures can be loaded from external files with very little code.
/*
*
* Udemy.com
* Reading from Files in C Using Fread
*
*/
#include
#pragma pack(1)
struct TEST {
char first;
short second;
char third;
} myTest;
#pragma pack()
int main( int argc, const char* argv[] ) {
FILE *tf;
printf("\r\nUdemy.com - Reading from Files in C\r\n");
tf = fopen("Test.txt", "r");
fread(&myTest, 1, 4, tf);
fclose(tf);
printf("First: %c\r\n", myTest.first);
printf("Second: %u\r\n", myTest.second);
printf("Third: %c\r\n", myTest.third);
}
When C compilers create structures, they often pad them to a size that is convenient for them to work with. To
successfully read the test structure from the text file, you may need to instruct the compiler to use a different
packing scheme in this case, using the pragma directive pack(1) to tell the compiler not to pad the structure.
The process for setting a structures packing mode may differ among C compilers.
Learn more about the common C functions for file handling in Learn C
Programming Language at Udemy.com.
https://blog.udemy.com/fread-c/
3/4
16/4/2015
Return to top of page
https://blog.udemy.com/fread-c/
4/4