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" Code:
go=`cat /tmp/file_test` # "cat /tmp/file_test" read the content of the file_test |
Quote:
|
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? |
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 |
Quote:
|
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?
|
Quote:
|
Code fix!
Code:
#include <stdio.h> |
Quote:
|
Code Fix!
|
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) Code:
if file_exists(/tmp/somefile) |
Quote:
Quote:
Code:
FILE *fp; |
All times are GMT -5. The time now is 10:54 AM. |