Hello, I was trying to implement data structures using file operations. I am using a data structure of two integers, one being the index number and other being the value. My actual aim is to recreate the file containing the data using a log. But, the program isn't running as required. Please help.
I am giving the code:

ifstream rlist;
   	rlist.open("redo.txt",ios::in);
   	ofstream datt;
   	datt.open("datatemp.txt",ios::out);
   	dat c;
   	char s;

   	while(!rlist.eof()){
         rlist>>s;
         if(s=='I'){
         		rlist.read((char*) &c,sizeof c);
            	datt.write((char*) &c,sizeof c);
               }
         if(s=='D'){
         		ifstream rlistd;
   				rlistd.open("datatemp.txt",ios::in);
               rlist.read((char*) &c,sizeof c);
            	ofstream rlistdw;
      			dat s;
               rlistdw.open("datatem.txt",ios::out);

   				while(!rlistd.eof()){
         			rlistd.read((char *) &s, sizeof s);
         			if((s.a)!=(c.a))
      					rlistdw.write((char*) &s, sizeof s);
                  }
      			rlistd.close();
      			rlistdw.close();
      			remove("datatemp.txt");
      			rename("datatem.txt","datatemp.txt");
      			}

Here, "redo.txt" is my log file. And dat is the structure name. The insert(s=='I') thing is working correct but there is some problem with delete one.
And is that true that we can't access the same file using two file pointers at the same time.
Waiting for reply...

Recommended Answers

All 5 Replies

Why is it new people always think that "something is wrong" is an explanation of the problem? If you take your car to get fixed, will "something is wrong" be enough for the guy to correct the problem?

Details! What is wrong? What did you expect to happen? What actually happened?

And clean up your formatting! the code is a mess and hard to follow. See this.

And yes, it's true. You can't

Sorry, for the above way of posting. Actually, I didn't want to give a large code, so I gave just a small part of it.
Though, most of my problem was solved when you told that we can't access the same file from 2 pointers at the same time, and I am very thankful of you for your help. But, now I am having a new problem;
In the following code:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>

struct dat{                     //This is the structure that I want to enter into my file
	int a,b;
};
int point=0;


 void read(){                                  //This whole function deals with reading
   	clrscr();                                  //data from file:"data.txt"
   	ifstream fr;

   	dat s;
   		fr.open("data.txt",ios::in,ios::beg);
   		while(fr.eof()==0){
   			fr.read((char *) &s, sizeof s);    //This is to read from the file, the data
   			printf("%d\t%d\n",s.a,s.b);
   		}
               fr.close();
   	getch();
   }

void insert(){                                   //This function inserts data in file
		dat v;
   	ofstream f;
   	clrscr();
   	if(point==0){                                   //I have used this if-else structure to see
	   	f.open("data.txt",ios::out);    //if the file is opened for first time. 
      	point=1;                                         //If the file is opened first time,
      	}                                                     //it opens it in out mode, else in append mode.
   	else{
   		f.open("data.txt",ios::app);
      	}
  		printf("Enter the data:\n");
  		cin>>v.b;
   	cout<<"\nData index number:"<<v.a<<"\nData value:"<<v.b<<"\n";
   	f.write((char*) &v, sizeof v);
   	v.a++;
   	f.close();
   	getch();
   	}

void main(){
insert();
read();
}

My aim is very simple. It is to input a structure in the file and then to read it back.
Now, the problem I am facing is that when I read the file, I am shown the value two times. And particularly, when I insert more than 1 data item, the program reads just the last value twice, all other are read normally(once).
Please help, and maybe I could state my problem more simpally now.

Now, the problem I am facing is that when I read the file, I am shown the value two times.

Simply don't use .eof() in the loop control, it does not work as you may think it does. Rather do..

while (fr.read((char *) &s, sizeof s))
{
  printf(...);
}

Maybe see Why does my input seem to process past the end of file?

Simply don't use .eof() in the loop control, it does not work as you may think it does. Rather do..

while (fr.read((char *) &s, sizeof s))
{
  printf(...);
}

Maybe see Why does my input seem to process past the end of file?

The above link and your explanation has solved my problem and I am very thankful to you for that.
But I am not able to understand that when I try to read from the file when the get pointer is at the last byte (or somwhere there) of the file then why does it read the last data element entry full. I meant that when the get pointer has already traversed the last entry then why is it travelling back a few bytes to read that again. Please explain???
And sorry for the late reply...

But I am not able to understand that when I try to read from the file when the get pointer is at the last byte (or somwhere there) of the file then why does it read the last data element entry full. I meant that when the get pointer has already traversed the last entry then why is it travelling back a few bytes to read that again. Please explain???
And sorry for the late reply...

We don't care how late your reply was? We weren't waiting for it, believe me. :icon_wink:

See this about your .eof() question. feof() is identical to .eof().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.