LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-21-2005, 12:40 PM   #1
Shing
LQ Newbie
 
Registered: Mar 2005
Location: Dublin, Ireland
Posts: 8

Rep: Reputation: 0
Unhappy Pointers To Write To Files


Hi,

I have two programs that read the Serial Port buffer and write the data in two text files respectively.

I use fopen() and fclose(), passing pointers, as parameters to access the Serial Port in each program.

When I execute the first program, the data is written into the first text file, which is normal. However, when I execute the second program, the data is written into the first text file again. If I execute the second program a second time, the data is then written into the second text file.

I am running Mandrake and programming in C. Does anybody know what I could be doing wrong?

Regards,
Wai Shing

Last edited by Shing; 03-21-2005 at 12:41 PM.
 
Old 03-21-2005, 12:46 PM   #2
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
You could be using the same variable to address the filename... Try naming the filename variable differently (if that sollves your problem) or try clearing before reusing it.
 
Old 03-21-2005, 12:57 PM   #3
Shing
LQ Newbie
 
Registered: Mar 2005
Location: Dublin, Ireland
Posts: 8

Original Poster
Rep: Reputation: 0
I have different pointer variables addressing the filename (namely "temperature" and "humidity"). But the pointers to the Serial Port are of the same name in both programs. Maybe that could be the cause of the problem. I'll check it out. Thanks!

Regards,
Wai Shing
 
Old 03-21-2005, 03:19 PM   #4
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Shing:

If you're still having troubles, try posting a code snippet.
 
Old 03-21-2005, 03:54 PM   #5
Shing
LQ Newbie
 
Registered: Mar 2005
Location: Dublin, Ireland
Posts: 8

Original Poster
Rep: Reputation: 0
I originally wanted to do that but I know people don't quite like debugging other people's code.

Three pointers are declared in each of the two programs. One program reads a temperature value from the serial port buffer. The other program reads a humidity value.

Code:
FILE *temperature;
FILE *port_read;
FILE *port_write;
The two programs are identical except in the second program, the first pointer is declared as
Code:
FILE *humidity;
Pointer temperature points to a text document where the data is written into.
Pointer port_read points to the serial port used when reading.
Pointer port_write points to the serial port used when writing.

Reading the serial port is done by
Code:
fscanf(port_read,"%c",&data);
Writing to the serial port is done by
Code:
fprintf(port_write,"T");
Writing to the text file is done by
Code:
fprintf(temperature,"%c",data);
Hope that is helpful.

Regards,
Wai Shing

Last edited by Shing; 03-22-2005 at 03:32 AM.
 
Old 03-22-2005, 05:02 AM   #6
Shing
LQ Newbie
 
Registered: Mar 2005
Location: Dublin, Ireland
Posts: 8

Original Poster
Rep: Reputation: 0
OK, I renamed the pointers in the two programs so that no two variables names match. However, the problem still exists. For some reason, the first piece of data read is always written into the file that was accessed last.

Here is a description of what happens when I execute the programs.
1) When I execute measure_temperature.exe, a blank file called "temperature.txt" is created.
2) When I execute it a second time, only then is the data written into the text file.
3) When I execute measure_humidity.exe, a blank file called "humidity.txt" is created but the data is written into "temperature.txt".

This is the code of measure_temperature.exe in it's entirety.

Code:
#include <fcntl.h>
#include <stdio.h>

int main(void)
{
	unsigned int counter=0;
	unsigned char data=0;
	
	FILE *temperature;		//Pointer to "temperature" of type file.
	FILE *port_read_temperature;	//Pointer to "port" of type file.
	FILE *port_write_temperature;	//Pointer to "port" of type file.

	//Open the Serial Port (COM1).
	port_write_temperature=fopen("/dev/ttyS0","w");	

	//Error Handling.
	if(port_write_temperature==NULL)
	{
		perror("fopen");		//Print Error.
		exit(1);			//Exit Program.
	}
	
	fprintf(port_write_temperature,"T");	//Send Character "T" to the Serial Port.
				
	fclose(port_write_temperature);

	//Open a DAT file to write temperature values into.
	temperature=fopen("temperature.txt","w");		
	
	//Open the Serial Port (COM1).	
	port_read_temperature=fopen("/dev/ttyS0","r");		
		
	fscanf(port_read_temperature,"%c",&data);
	
	//While 'Y' is not received.
	while(data!='Y')
	{
		fprintf(temperature,"%c",data);	//Print Data to DAT file.
		
		fscanf(port_read_temperature,"%c",&data);	
	}
	
	fclose(port_read_temperature);	//Close Serial Port.
	fclose(temperature);		//Close DAT file.
	
	return 0;
}
 
Old 03-22-2005, 08:20 PM   #7
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Hint:
Explore the need for a new line charector.
It sometimes flushes the buffer.
 
Old 03-23-2005, 06:55 AM   #8
Shing
LQ Newbie
 
Registered: Mar 2005
Location: Dublin, Ireland
Posts: 8

Original Poster
Rep: Reputation: 0
By use of a newline character, I presume that you mean inserting a "\n" in the code. I have tried that but it does not seem to make a difference. I am guessing it is jumping to a newline in the text file.

Regards,
Wai Shing

Last edited by Shing; 03-23-2005 at 08:37 AM.
 
Old 03-31-2005, 04:45 AM   #9
Shing
LQ Newbie
 
Registered: Mar 2005
Location: Dublin, Ireland
Posts: 8

Original Poster
Rep: Reputation: 0
Still no resolve on this problem. The problem does not exist when I program it in Windows.

Could it be perhaps since the two programs were done as root, the text files are stored in the root directory that is causing the problem?

Regards,
Wai Shing
 
  


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
Able to write files to MP3 player but no files written into actual device? olnex Linux - Hardware 0 11-11-2005 06:32 AM
Can not write files to /home TigerLinux Mandriva 8 10-04-2005 06:19 PM
Cannot Write/Edit files on hd.. Mmc245 Linux - Hardware 3 07-03-2005 06:08 PM
How to write multiple c++ files? Xiangbuilder Programming 2 09-15-2003 09:12 AM
clients can write to files but they cannot delete them sparsh Linux - Networking 1 05-10-2003 09:54 PM

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

All times are GMT -5. The time now is 03:25 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
Open Source Consulting | Domain Registration