LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to check if the content of a file is "1"? (https://www.linuxquestions.org/questions/programming-9/how-to-check-if-the-content-of-a-file-is-1-a-787668/)

fbs777 02-07-2010 08:16 PM

how to check if the content of a file is "1"?
 
Question in C Language:

i want to close the program if the content of the file /tmp/file_name is 1 (just the number 1).

dont need to check all the time, just when start the program.

something like this:
Code:

"read the /tmp/file_name"
"check if the content of the file_name is 1"
"if not, do something"

what i need is very simple to do in shell script:
Code:

go=`cat /tmp/file_test`                # "cat /tmp/file_test" read the content of the file_test
if [ "$go" -eq "1" ]; then              # if the content of the file_test is 1
echo "ok, ready to exit now";          # now i can put the exit command
fi

but i dont know how to do the same thing in C...

Sergei Steshenko 02-07-2010 09:23 PM

Quote:

Originally Posted by fbs777 (Post 3856162)
...
but i dont know how to do the same thing in C...

What do you know in "C" ? I.e. what kinds of programs have you written ?

graemef 02-07-2010 09:57 PM

Look at the functions:

fopen()
fread()
fclose()

Focus not just on the arguments that you need to pass to it but also on the return values.

That might get you going but as Sergei has said, what do you already know about C?

tungvs 02-07-2010 10:08 PM

That would be very basic in C programming. you should try some "Hello world" example and tutorial before post this :).
I recommend this:
Code:

http://www.cprogramming.com/tutorial.html#ctutorial

Sergei Steshenko 02-07-2010 10:15 PM

Quote:

Originally Posted by graemef (Post 3856213)
Look at the functions:

fopen()
fread()
fclose()

Focus not just on the arguments that you need to pass to it but also on the return values.

That might get you going but as Sergei has said, what do you already know about C?

Then you should have also mentioned 'fstat' :) - I think. Because there is an implied requirement of fixed file size and 'fstat' is a very straightforward way to get it.

bartonski 02-07-2010 10:19 PM

In C, 1 can be represented as either the integer 1 or the character '1'. How you read the number depends on how it's represented. Can you elaborate?

Sergei Steshenko 02-07-2010 10:20 PM

Quote:

Originally Posted by bartonski (Post 3856235)
In C, 1 can be represented as either the integer 1 or the character '1'. How you read the number depends on how it's represented. Can you elaborate?

Shell code example published by the OP suggests ASCII.

smeezekitty 02-07-2010 11:17 PM

Code fix!
Code:

#include <stdio.h>
int main()
{
FILE *in = fopen("tf", "rb");
unsigned char d = fgetc(in);
fgetc(in);
if(d == '1' && feof(in)){ } else { }
fclose(in);
return 0;
}


fbs777 02-07-2010 11:51 PM

Quote:

Originally Posted by smeezekitty (Post 3856277)
int main()
{
FILE *in = fopen("<FILE NAME HERE>", "rb");
unsigned char d = fgetc(in);
if(d == '1' && feof(in)){ } else { }
fclose(in);
return 0;
}

thank you, i will test this code tomorrow :)

smeezekitty 02-08-2010 02:35 AM

Code Fix!

b0uncer 02-08-2010 02:46 AM

If the contents of the file will always be 1 (nothing else) in your case of interest, wouldn't it be just the same to create an empty file with a name ending in 1? Otherwise a file called (just) "1" would do, but you shouldn't rely on that being a unique name, especially in /tmp, however unprobable it feels. So if you have to create the file anyway, and you know the (static?) name of it in case of your interest, you do not need to actually read the file to see if it's contents are "1" or something else; just check whether or not it exists (and maybe if it's new enough?).

Edit: (pseudo code) originally you wanted to do something like this:
Code:

file = open(/tmp/somefile)
contents = read_contents(file)
close(file)
if contents == 1
  do something
else
  do something else

but you could also do it like this:
Code:

if file_exists(/tmp/somefile)
  do something
else
  do something else

It might get your code shorter, if you really only need to check against one value. But then again, if you cannot change the way your "flag" is created, i.e. the other end that writes the file, then you'll just have to live with it. Hopefully this clarified my thoughts...

fbs777 02-15-2010 07:26 AM

Quote:

Originally Posted by smeezekitty (Post 3856277)
Code fix!
Code:

#include <stdio.h>
int main()
{
FILE *in = fopen("tf", "rb");
unsigned char d = fgetc(in);
fgetc(in);
if(d == '1' && feof(in)){ } else { }
fclose(in);
return 0;
}


thank you, but for some reason dont worked here...

Quote:

Originally Posted by b0uncer
but you could also do it like this:
Code:

if file_exists(/tmp/somefile)
  do something
else
  do something else

It might get your code shorter, if you really only need to check against one value. But then again, if you cannot change the way your "flag" is created, i.e. the other end that writes the file, then you'll just have to live with it. Hopefully this clarified my thoughts...

yes, thank you, and reading a book i found a way to work:

Code:

FILE *fp;
fp=fopen ("/tmp/file_name","r");
if (!fp)
    {
      exit (1);
    }
fclose(fp);

thank you all :)


All times are GMT -5. The time now is 10:54 AM.