How to read a file and compare values in C? - c

I need to know how could I read a file with values in different lines, and compare it with values I have in memory, to get wich line has a better score, and if values in memory are best, insert those values into the file (the file is open as read and as write at the same time, as this part of code could be run from several fork() at the same time):
if (PuntuacioEquip(jugadors)>MaxPuntuacio && CostEquip(jugadors)<PresupostFitxatges)
{
//Compare with file
int fd, n;
TBestEquip info;
fd = open("best_teams.txt", O_RDONLY);
if (fd<0) panic("open");
while ((n=read(fd, &info, sizeof(info))) == sizeof(info)) {
//printf(cad,"equip %lld, puntuacio %d\n", info.Equip, info.Puntuacio);
//write(1,cad,strlen(cad));
if (info.Puntuacio>PuntuacioEquip(jugadors))
{
fd = open("best_teams.txt", O_WRDONLY|O_TRUNC|O_CREAT,0600);
if (fd<0) panic("open");
sprintf(cad,"%s Cost: %d Points: %d. %s\n", CostEquip(jugadors), PuntuacioEquip(jugadors));
write(fd,cad,strlen(cad));
}
}
// We have a new partial optimal team.
MaxPuntuacio=PuntuacioEquip(jugadors);
memcpy(MillorEquip,&jugadors,sizeof(TJugadorsEquip));
sprintf(cad,"%s Cost: %d Points: %d. %s\n", color_green, CostEquip(jugadors), PuntuacioEquip(jugadors), end_color);
write(1,cad,strlen(cad));
}
Appreciate any help.
Regards,

The best way to iterate through a file is to use the function getline(). Here is an example of its utilisation, taken from this post which I advise you read.
char const* const fileName = "best_teams.txt" ; //
FILE* file = fopen(fileName, "r"); /* should check the result */
if (file != NULL) {
char line[256];
while (getline(line, sizeof(line), file)) { // Each iteration, a line will be stored in string `line`
// Do what you want to do
} // Exits when arrives at the end of the file
else puts("Error while opening file\n");
As suggested in the comments, you can then use fopen("best_teams.txt", "w")
The "w" means write mode, which is described as follow in fopen documentation :
Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file.
Another solution is to open in read and write mode, and to only change the values you desire, but it might be more complicated.

Related

Check if a file is empty or not

How can I check if a text file has something written or not. I tried:
LOGIC checkfile(char * filename)
{
FILE *pf;
pf=fopen(filename,"wt");
fseek(pf,0,SEEK_END);
if(ftell(pf)==0)
printf("empty");
}
This function returns empty everytime, even in my text file I have few words or numbers written.
The problem is that you opened the file for writing. When you do that, everything in the file is lost, and the length of the file is truncated to 0.
So you need to open the file for reading. And the easiest way to see if the file is empty is to try to read the first character with fgetc. If fgetc returns EOF, then the file is empty.
First of all: DO NOT OPEN THE FILE FOR WRITING!
second: for knowing about file status in C you can use fstat which is in sys headear file!
You can use struct stat for using this function
here is a simple example:
#include <sys/stat.h>
int main(void)
{
int fields = 0;
int size = 0;
// Open the file test.txt through open()
// Note that since the call to open directly gives
// integer file descriptor so we used open here.
// One can also use fopen() that returns FILE*
// object. Use fileno() in that case to convert
// FILE* object into the integer file descriptor
if(fields = open(file_path, "r")){
struct stat buf;
fstat(fields, &buf);
size = (int)buf.st_size;
}
printf("size of file is %d", size);
return 0;
}
Note: I just include a header file that related to fstat. You can add other header files yourself
What about using fscanf to read the file, and check if something was actually read?
FILE *fp;
char buff[255] = "";
fp = fopen(filename, "r");
fscanf(fp, "%s", buff);
if (!*buff)
printf("Empty\n");
else
printf("%s\n", buff);
fclose(fp);

Chmod in C assigning wrong permissions

The following is my code for a method that copies a file from a path to a file to a directory provided as the destination. The copy works perfectly fine, however my chmod call assigns the wrong permissions to the copied file in the destination. If the permission in the source is 644, the copied file has a permission of 170 or 120.
I have been attempting to debug this for hours and it's driving me slightly crazy so any help is greatly appreciated.
void copy_file(char* src, char* dest) {
char a;
//extract file name through a duplicate ptr
char* fname = strdup(src);
char* dname = basename(fname);
//open read and write streams
FILE* read;
FILE* write;
read = fopen(src, "r");
chdir(dest);
write = fopen(dname, "w");
//error checking
if (read == NULL) //|| (write == NULL))
{
perror("Read Error: ");
exit(0);
}
else if (write == NULL)
{
perror("Write Error: ");
exit(0);
}
//write from src to dest char by char
while (1){
a = fgetc(read);
if (a == EOF)
{
break;
}
fputc(a, write);
}
//close files
fclose(read);
fclose(write);
// this is where I attempt to assign source file permissions
//and it goes horribly wrong
struct stat src_st;
if(stat(src, &src_st)){
perror("stat: ");
}
chmod(dname, src_st.st_mode);
printf("%o\n", src_st.st_mode & 0777);
}
You fopen(src, "r"), then you chdir(dest). This means that when you later call stat(src, &src_st), there is no reason to think that stat will access the same file as fopen did, or indeed that stat will access any file at all.
If stat fails, you proceed to call chmod anyway, so you pass whatever random junk was in src_st.st_mode to chmod.
You should use fstat(fileno(read), &src_st) before calling fclose(src), instead of calling stat(src, &src_st).
The basic problem is you have to check your system calls like fopen, chdir, and stat immediately.
For example, first thing I tried was copy_file( "test.data", "test2.data" ) not realizing it expected a destination directory.
char* fname = strdup(src);
char* dname = basename(fname);
dname is now test.data, same as the source.
read = fopen(src, "r"); // succeeds
chdir(dest); // fails
write = fopen(dname, "w"); // blows away test.data, the source
You do eventually check read and write, but after the damage has been done.
Blowing away your source file is really bad. It's important that your code deals with failed system calls. If you don't, it will sail along causing confusion and destruction.
Most system calls in C return 0 for success. This is an anti-pattern where the return value is an error flag, so false is failure, and anything else indicates what kind of error (though stat doesn't use that, it uses errno).
When it fails, stat returns -1 which is true. So this is the wrong way around.
struct stat src_st;
if(stat(src, &src_st)){
perror("stat: ");
}
Instead, you have to check for non-zero.
struct stat src_st;
if(stat(src, &src_st) != 0 ){
// Note that I don't use perror, it doesn't provide enough information.
fprintf(stderr, "Could not stat %s: %s\n", src, strerror(errno));
exit(1);
}
As you can guess this gets tedious in the extreme, and you're going to forget, or do it slightly different each time. You'll want to write wrappers around those functions to do the error handling for you.
FILE *fopen_checked( const char *file, const char *mode ) {
FILE *fp = fopen(file, mode);
if( file == NULL ) {
fprintf(stderr, "Could not open '%s' for '%s': %s", file, mode, strerror(errno));
exit(1);
}
return fp;
}
It's not the best error handling, but it will at least ensure your code appropriately halts and catches fire.
A note about chdir: if you can avoid it don't use it. chdir affects the global state of the program, the current working directory, and globals add complexity to everything. It's very, very easy for a function to change directory and not change back, as yours does. Now your process is in a weird state.
For example, if one did copy_file( "somefile", "foo" ) this leaves the program in foo/. If they then did copy_file( "otherfile", "foo" ) they'd be trying to copy foo/otherfile to foo/foo/otherfile.
And, as #robmayoff pointed out, your stat fails because the process is now in a different directory. So even the function doing the chdir is confused by it.
Ensuring that your functions always chdir back to the original directory in a language like C is very difficult and greatly complicates error handling. Instead, stay in your original directory and use functions like basename to join paths together.
Finally, avoid mixing your file operations. Use filenames or use file descriptors, but try not to use both. That means if you're using fopen, use fstat and fchmod. You might have to use fileno to get a file descriptor out of the FILE pointer.
This avoids having to carry around and keep in sync two pieces of data, the file descriptor and the filename. It also avoids issues with chdir or the file being renamed or even deleted, the file descriptor will still work so long as it remains open.
This is also a problem:
char a;
...
while (1){
a = fgetc(read);
if (a == EOF)
{
break;
}
fputc(a, write);
}
fgetc() returns int, not char. Per the C Standard, 7.21.7.1 The fgetc function:
7.21.7.1 The fgetc function
Synopsis
#include <stdio.h>
int fgetc(FILE *stream);
Assuming sizeof( int ) > sizeof( char ), char values are signed, 2s-complement integers, and EOF is an int defined to be -1 (all very common values), reading a file with char a = fgetc( stream ); will fail upon reading a valid 0xFF character value. And if your implementation's default char value is unsigned char, char a = fgetc( stream ); will never produce a value that matches EOF.

C - Printing Bin. File In Weird Symbols

I created a function that is successfully reading the binary file but it is not printing as I wanted.
The function:
void print_register() {
FILE *fp;
fp = fopen("data.bin", "rb");
if (fp == NULL) {
error_message("Fail to open data.bin for reading");
exit(0);
}
reg buffer;
while (EOF != feof(fp)) {
fread(&buffer, sizeof(reg), 1, fp);
printf("%s %d %d\n", buffer.name, buffer.age, buffer.id);
}
fclose(fp);
}
Note: reg is a typedef for a struct:
typedef struct registers reg;
struct registers {
char name[30];
int age;
int id;
char end;
};
Function for writing the file:
void register_new() {
system("clear");
reg buffer;
FILE *fp;
fp = fopen("data.bin", "ab");
if (fp == NULL) {
error_message("Error opening file data.bin");
exit(0);
}
write_register(buffer);
fwrite(&buffer, sizeof(reg), 1, fp);
fclose(fp);
}
Posting a printscreen of what was print to be more helpful:
As you can see on image, after the "p" (command for printing) is where should be the name, age and id of the struct.
In register_new(), you have to send the address of buffer in order for write_register() to work properly (right now you're giving it a copy of buffer).
Replace:
write_register(buffer);
with:
write_register(&buffer);
Then correct write_register to take and work with an address instead of a structure.
This might help you understand what's going on: http://fresh2refresh.com/c-programming/c-passing-struct-to-function
Your reading loop is incorrect. Don't use feof(), it can only tell is you have reached the end of file after a read attempt failed and it might not return EOF anyway, it is only specified as returning 0 or non 0. Use this instead:
while (fread(&buffer, sizeof(reg), 1, fp) == 1) {
printf("%s %d %d\n", buffer.name, buffer.age, buffer.id);
}
fread returns the number of items successfully read. Here you request to read 1 item of size sizeof(reg), if the item was read successfully, fread will return 1, otherwise it will return 0 (in case of a read error or end of file reached).
Your screenshot shows a syntax error, which you seem to have fixed now. Remove that, it is not helping.
In your function register_new, you are writing an uninitialized structure reg to the file, no wonder it does not contain anything useful when you read it back from the file. And for what it is worth, opening this file in binary mode is the correct thing to do since it contains binary data, namely the int members of the structure.
The reg passed to fwrite is indeed uninitialized. write_register gets a copy of this uninitialized structure by value, and probably modifies this copy, but this does not affect the local structure in register_new. You should modify write_register() to take a pointer to the structure. Unlike C++, there is no passing by reference in C.

Trouble reading and writing to files in C

I have written the following code (as part of ab assignment for university) in an attempt to save 1 int to a text file (using fprintf) and an array of structs of type Flight to a .bin file. It seems that both are remaining empty. I am calling the read at the beginning of the program and the write on exit or when the Save option is selected. The read must first take the int value as it is a crucial counter for how many elements there are (and therefore how many need to be read / written).
I have looked at other answers, and even based some of my code off them however after reading and re-reading I still could not find a solution hence i posted a new question.
This is the Write Section, The files are flyC.txt (to store the counter) and fly.bin to store the array of structs.
void writeFlight (){
FILE * cpt;
if ((cpt = fopen("flyC.txt", "wb")) == NULL)
{
printf("ERROR: Flight Count File Could Not Be Opened / Written To \n");
}
fprintf(cpt, "%d", curFly);
FILE * fpt;
if ((fpt = fopen("fly.bin", "wb")) == NULL)
{
printf("ERROR: Flight File Could Not Be Opened / Written To \n");
}
fwrite(flyList, curFly * sizeof(struct Flight), 1, fpt);
fclose(fpt);
}
And here is the Read section.
void readFlight(){
//First Read file with variable curFly
FILE * cpt;
if((cpt = fopen("flyC.txt", "rb")) == NULL)
{
printf("ERROR: Flight Count File Could Not Be Opened\n");
}
if (1 != fscanf(cpt, "%d", &curFly)){
printf("ERROR: Flight Count File Could Not Be Read\n");
}
fclose(cpt);
FILE * fpt;
if((fpt = fopen("fly.bin", "rb")) == NULL)
{
printf("ERROR: Flight File Could Not Be Opened / Read\n");
}
fread(flyList, sizeof(struct Flight) * curFly, 1, fpt);
fclose(fpt);
}
Any help is appreciated!
I see several issues here.
I understood that flyC.txt was supposed to contain plain ASCII text rather than binary data. If this is the case, than you shouldn't use "wb" when opening cpt for writing, but just "w"
While writing cpt you don't close it. The OS might close it for you at the end of the program, but you shouldn't rely on this.
While reading the file back, you've done a similar mistake for flyC.txt; you shouldn't open it using “rb" but just "r" if it contained binary data.
You forgot fclose(cpt); in the write function.
Also you should return on error and not call fread/fwrite.

reading a port (GPIO) value in C (beagleboard-xm)

I have an embedded board (beagleboard-xm) that runs ubuntu 12.04. I need to read a GPIO continuously to see if the value of the port changes. My code is herebelow:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
FILE *fp;
int main(void)
{
//linux equivalent code "echo 139 > export" to export the port
if ((fp = fopen("/sys/class/gpio/export", "w")) == NULL){
printf("Cannot open export file.\n");
exit(1);
}
fprintf( fp, "%d", 139 );
fclose(fp);
// linux equivalent code "echo low > direction" to set the port as an input
if ((fp = fopen("/sys/class/gpio/gpio139/direction", "rb+")) == NULL){
printf("Cannot open direction file.\n");
exit(1);
}
fprintf(fp, "low");
fclose(fp);
// **here comes where I have the problem, reading the value**
int value2;
while(1){
value2= system("cat /sys/class/gpio/gpio139/value");
printf("value is: %d\n", value2);
}
return 0;
}
the code above reads the port continuously (0 by default), however, when I change the port as 1, system call output the correct value, however printf still prints 0 as an output. What is the problem with value2 that does not store the value that system() outputs.
If I use the code below instead of while loop above, I get an error about opening the value file (Cannot open value file.), if I put the fopen line outside of while loop, it does not show the changes in the value file.
char buffer[10];
while(1){
if ((fp = fopen("/sys/class/gpio/gpio139/value", "rb")) == NULL){
printf("Cannot open value file.\n");
exit(1);
}
fread(buffer, sizeof(char), sizeof(buffer)-1, fp);
int value = atoi(buffer);
printf("value: %d\n", value);
}
My question: how do I need to fix the code? or how should I read the value file?
As an additional info that I wonder: what is the difference to e.g. export the port by system("echo 139 > /sys/class/gpio/export") and fp = fopen("/sys/class/gpio/export","w"); fprintf(fp,"%d",139); which method do you suggest me to use? Why?
Thank you in advance.
The system() function returns the return value of cat, which is 0. It does not return the standard output from cat, which is what you were expecting.
I think the problem with your second bit of code is that you're not calling fclose(). Since you're running in a tight loop, you almost immediately exceed the number of open files allowed.
So, call fclose(), and think about putting a sleep() in there too.
When reading a file in C, your position in the file changes as you read it. For example, if you were to open a file with the contents:
First Line
Second Line
Third Line
and run this program:
char buffer[1024];
while(fgets(buffer, sizeof(buffer), theFile))
{
printf("Buffer: %s", buffer);
}
It would print:
First Line
Second Line
Third Line
As you read each line, the position in the file changes to the next line.
In your program, after reading the value the first time, you are trying to read the empty space in the file, instead of the value you want.
The solution to your problem is to move fopen outside of the while loop, but call fseek to reset your position to the start of the file each time through the loop.
To use fseek, you need to pass it a file pointer, byte offset and seek point. Here you call it on your file, with a 0 byte offset from the waypoint SEEK_SET, which indicates the start of the file.
fseek(theFile, 0, SEEK_SET);

Resources