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-06-2013, 09:08 AM   #1
blavo
LQ Newbie
 
Registered: Jun 2004
Distribution: Suse 10
Posts: 22

Rep: Reputation: 0
problems writing to a file.


I’m working on an embedded Linux platform and I’ve created an application in C++. The application creates and opens 3 data files using;
1 files declared global
ofstream datafileA;
ofstream datafileB;
ofstream datafileC;

2 files opened in main()
datafileA.open("rawdataA.dat");
datafileB.open("rawdataB.dat");
datafileC.open("rawdataC.dat");

3 write data to the files in add_data()
datafileA <<dataA<<" , "<<"count= "<< count<<endl;
datafileB << dataB<<" , "<<"count= "<< count<<endl;
datafileC << dataC <<" , "<<"count= "<< count<<endl;

4. close files
datafileA.close()
datafileB.close()
datafileC.close()

The application is very simple, it just runs in forever loop writing data to file until the user hits cntrl + c to end the application. Consistantly datafileB and datafileC hold quite a few points, something that looks realistic to the amount of time that the application has been running. DatafileA on the other hand, seems to quit loging after only a small number of datapoints?

Three files all handled identically, data retrieved and written to identically. I can’t seem to figure out why datafileA is consistently not working. Any help is greatly appreciated.
 
Old 03-06-2013, 10:02 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
You should show smallest excerpt from your code which is not working, because with this code isn't anything wrong. Stopping an application with CTRL+C is not a good method, if you does not handle this signal (it is SIGINT) in your application and gracefully close all file handles, etc. Can you confirm that data saved in this file is different than for example displayed simultaneously on the screen?
 
Old 03-06-2013, 10:48 AM   #3
blavo
LQ Newbie
 
Registered: Jun 2004
Distribution: Suse 10
Posts: 22

Original Poster
Rep: Reputation: 0
It's funny you ask about printing to the screen as well to verify if the data is still available. I actually did that experiment to test it, in fact the three data points do continue printing to the screen. The B and C files continue to get updated while the A file stops after short value.

I agree that I'm not handling the closing of the application and files very gracefully yet, that could be my problem? I would expect the same problems with all files, not just one.

I'll put together an excerpt and post it, but what I posted was basically a cut and paste of file handling throughout the code.

I'm going to work on the signal( SIGINT) now to see if that works.

Thanks for your help with this.
 
Old 03-07-2013, 01:29 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Have you tried rearranging the order of the writes?
Try another data file, i.e. a fourth see what happens.
Try using C stdio instead.
Are they open for append?

Try open, append, close, open, append, close etc...
 
Old 03-07-2013, 04:48 AM   #5
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
So if data on screen and on file are the same then there is no problem with writing file. The bug most probably is elsewhere. Can you show how this data is readed and written?
 
  


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
NFS Large File Copies Fail - Error writing to file: Input/output error deck- Linux - Server 10 08-01-2022 02:30 AM
reading and writing to pipes, file descriptors, and file streams cmartin0 Programming 1 02-13-2012 03:03 AM
Error writing to file: File too large gaviono Linux - Server 3 01-23-2010 09:32 PM
Writing an ISO file for cd audio from a cue and wave file? spaz-atk Linux - Software 5 12-01-2008 01:52 AM
problems while writing to device file montylee Programming 2 05-14-2008 11:55 PM

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

All times are GMT -5. The time now is 04:10 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