LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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-16-2004, 10:32 PM   #1
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Rep: Reputation: 30
writing to files in C


i am very new to C but making my way through.
im using calls to a shared lib for controlling a robotics microcontroller.
a function, defined in the header, like this:

aServo_GetPosition(stem, GPMOD, SRV2, srv1pos);

it gets the position of a servo and puts the value in srv1pos an 'unsigned char'.
the result is a number between 0 and 255.

printf("servo postion = %d\n", srv1pos);

yeilds # servo position = 178

so all i want to do is to put that '178' into a file.

i have defined another function where the file is to be written
and i want to pass 'srv1pos' as a variable to that function.
i've tried using fputc(), fputs() and fwrite() and i just can't get it to work.

thanks in advance for any help
 
Old 02-16-2004, 10:57 PM   #2
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Can you post any code?
 
Old 02-16-2004, 11:28 PM   #3
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
did you open the file first? (you have to open a defined file b4 you can do anyhting to it)
 
Old 02-16-2004, 11:30 PM   #4
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
Have u checked the return values of those fp.. functions.
Post your code than only i can help you.
 
Old 02-17-2004, 08:18 AM   #5
ac1980
Member
 
Registered: Aug 2003
Location: Trento, Italy
Distribution: Debian testing
Posts: 394

Rep: Reputation: 30
if you want it to be displayed in human-readable form you should use fprintf (same as printf but takes a filehandle too)
 
Old 02-17-2004, 09:51 AM   #6
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
so here is the relevant code.

/* filewriter */

int filewriter(char wfilename[], int wdata)
{
FILE *wstemdev;
char buff;
buff = wdata;
enum {WSUCCESS, WFAIL};
int wreval = WSUCCESS;
if ((wstemdev = fopen(wfilename, "w")) == NULL){
wreval = WFAIL;
return 1;
}
else
{
fputc( buff, wstemdev);
fclose(wstemdev);
}
}

/* this gets the servo position */

iwErr = aServo_GetPosition(stem, GPMOD, SRV1, &srv0posloop);
filewriter("../stem/servo/servo-0", srv0posloop);


im going to try using fprintf() that sounds promising.
thanks for all replies
 
Old 02-17-2004, 09:55 AM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
fprintf("%d\n",buff);

will print '178' to the file.
 
Old 02-17-2004, 10:15 AM   #8
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
i got it.

int filewriter(char wfilename[], int wdata)
{
FILE *wstemdev;
enum {WSUCCESS, WFAIL};
int wreval = WSUCCESS;
if ((wstemdev = fopen(wfilename, "w")) == NULL){
wreval = WFAIL;
return 1;
}
else
{
fprintf( wstemdev, "%d\n", wdata);
fclose(wstemdev);
}
}

fprintf() was what i needed.
now i need to figure out how to get the same data back out.
i'll work on it for a bit before i bug you guys again.

thanks alot.
i love this place
 
Old 02-17-2004, 10:18 AM   #9
vinay_s_s
Member
 
Registered: Jul 2003
Posts: 659

Rep: Reputation: 30
er.. fscanf ??
 
Old 02-17-2004, 12:06 PM   #10
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
right so i'm trying to use scanf() like this


int filewriter(char wfilename[], int wdata)
{
FILE *wstemdev;
enum {WSUCCESS, WFAIL};
int wreval = WSUCCESS;
if ((wstemdev = fopen(wfilename, "w")) == NULL){
wreval = WFAIL;
return 1;
}
else
{
fprintf( wstemdev, "%d\n", wdata);
fclose(wstemdev);
}
}

int main()
{
unsigned char s1l, srv1posloop;
while(1)
{
iwErr = aServo_GetPosition(stem, GPMOD, SRV1, &srv0posloop);
filewriter("../stem/servo/servo-0", srv0posloop);
FILE *dest0devr;
enum {SD0SUCCESS, SD0FAIL, SD0MAX_LEN = 81};
unsigned char sd0value[SD0MAX_LEN];
int sd0evalr = SD0SUCCESS;
if ((dest0devr = fopen("../stem/servo/dest-0", "r")) == NULL){
sd0evalr = SD0FAIL;
return;
}
else
{
fscanf(dest0devr, "%d" , &sd0value);
fclose(dest0devr);
iwErr = aServo_SetPositionAbs(stem, GPMOD, SRV1, sd0value);
}
}
}

but it doesn't work like expected
../stem/servo/dest-0 contains 127
the servo should move to match this number after it is gotten from the file
by fscanf()
but instead it moves to 224
all the values i put into files with aServo_GetPosition() and filewriter() are correct, but the values i get out with fscanf dont work properly
in aServo_SetPositionAbs().
 
  


Reply



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
writing files on to apache with php djgerbavore Programming 12 11-15-2004 02:01 PM
writing in read only files endezeichen Linux - Software 3 12-20-2003 03:48 PM
Command for writing over files? soulflyer Linux - Newbie 2 12-12-2003 05:26 PM
reading / writing OpenOffice files. GŠutama Programming 1 11-04-2003 06:46 AM
Writing Single files to a CD jimmmac Linux - Software 11 04-27-2003 04:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:55 AM.

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
Open Source Consulting | Domain Registration