LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-07-2010, 09:16 PM   #1
fbs777
LQ Newbie
 
Registered: Feb 2010
Posts: 3

Rep: Reputation: 0
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...

Last edited by fbs777; 02-07-2010 at 09:22 PM.
 
Old 02-07-2010, 10:23 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by fbs777 View Post
...
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 ?
 
Old 02-07-2010, 10:57 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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?
 
Old 02-07-2010, 11:08 PM   #4
tungvs
Member
 
Registered: May 2008
Distribution: Centos; Ubuntu; Fedora
Posts: 98

Rep: Reputation: 15
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
 
Old 02-07-2010, 11:15 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by graemef View Post
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.
 
Old 02-07-2010, 11:19 PM   #6
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
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?
 
Old 02-07-2010, 11:20 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by bartonski View Post
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.
 
Old 02-08-2010, 12:17 AM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,234

Rep: Reputation: 189Reputation: 189
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;
}

Last edited by smeezekitty; 02-08-2010 at 03:34 AM. Reason: CODE FIX!
 
Old 02-08-2010, 12:51 AM   #9
fbs777
LQ Newbie
 
Registered: Feb 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by smeezekitty View Post
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

Last edited by fbs777; 02-08-2010 at 12:54 AM.
 
Old 02-08-2010, 03:35 AM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,234

Rep: Reputation: 189Reputation: 189
Code Fix!
 
Old 02-08-2010, 03:46 AM   #11
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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...

Last edited by b0uncer; 02-08-2010 at 03:55 AM. Reason: Tried to clarify
 
Old 02-15-2010, 08:26 AM   #12
fbs777
LQ Newbie
 
Registered: Feb 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by smeezekitty View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] redirection of "ls" command to file gives chaotic content SharpyWarpy Linux - General 3 07-31-2009 04:13 AM
glibc 2.9 "make check" fails on rt/tst-cputimer with "timer sig[12] invoked to soon" shachter Linux - Software 2 02-14-2009 02:24 PM
"An error occurred during the file system check." JedSezZed Linux - Newbie 5 08-14-2008 01:23 AM
ld reports "no input file" at first sanity check (chapter 5.7) garba Linux From Scratch 1 07-23-2005 07:54 PM
"File system with errors, check forced" Luis Rosso Linux - General 5 02-02-2001 08:26 PM


All times are GMT -5. The time now is 06:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration