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 02-16-2005, 05:48 PM   #1
fuzzhead85
Member
 
Registered: Aug 2003
Posts: 42

Rep: Reputation: 15
c++ file handling issues


i was under the assumption that in c++ when you use the open command with ios::in|ios:ut, if the file does not exist, it will create one for you, and if you try and read in from that blank file, it should fill all the values of the record being read in, to nulls in a linux enviroment. here is a sample piece of code, that does not create a file and always returns read failed. any help would be great.

Code:
#include<iostream>
#include<fstream>
using namespace std;

class Block
{
public:
  int value1;
  int value2;
};

int main()
{
  fstream DVDM;
  Block test;
  DVDM.open("temp", ios::out|ios::in);
  test.value2 = -2;
  //idealy in this read, value2 will be set to null, and therefore 
  //the read would work
  DVDM.read((char*)&test, sizeof(Block));
  if(test.value2 == -2)
    cout << "read failed\n";
  else
    cout << "success\n";
  return 0;
}
 
Old 02-16-2005, 06:03 PM   #2
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
No, it won't create a file for you, try:

in | out | trunc

Secondly, read returns a reference to the stream - check the state fo this to see what has happened, either with:

if (your_stream) { /* oops error */ }

or with specific error checking functions e.g. good(), fail(), bad(), rdstate(), if you need to know more
 
Old 02-16-2005, 06:23 PM   #3
fuzzhead85
Member
 
Registered: Aug 2003
Posts: 42

Original Poster
Rep: Reputation: 15
the ios::trunc worked wonders, but why doesnt value2 get changed?, is there a way to initalize the file so that it will get changed?
 
Old 02-16-2005, 06:52 PM   #4
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Because there is nothing to read from!
You are asking to read some bytes from some underlying buffer which cannot provide them.

You would be better of checking the return and and then handling the error that way:

Code:
if (!DVDM.read((char*)&test, sizeof(Block)))
{
    test.value2 = 0; // or whatever ...
    DVDM.clear();    // need to clear the failbits before anything else can happen
}
Alternatively you might want to check how much is in the file with seekg(0, end) and tellg()
 
  


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
c file handling on linux suchi_s Programming 5 06-23-2004 09:01 AM
c file handling on linux suchi_s LQ Suggestions & Feedback 1 06-23-2004 01:48 AM
Screem file handling silmaril8n Linux - Software 1 06-14-2004 03:44 PM
file handling in c++ gtk+ alisha Programming 1 04-13-2004 12:22 PM
File handling in Linux Prommy Programming 7 07-21-2003 01:16 AM

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

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