hi,
i am having a problem with file input/output..the file created in the program using open command is not getting listed in the command line directory..also, the file created in one program cannot be retrieved in another program...how do i solve this problem??is this a problem with the operating system??

thanx in advance

Could you post down your code (please use code tags) and the contents of the file as well?

hi, i m new to this community and i actually dont know the use of code tags..
this is the program where i m trying to open rather access the file called inventory which is created in another program....inventory is a file which has records (name,no and price)...hope i m adressing the problem clearly..

#include<iostream>
#include<conio>
#include<iostream>
struct product
{
char name[20];
int no; 
int price;
};
ifstream fin;
void read_rec(product p)
{
fin.getline(p.name,20);
cin>>p.no>>p.price;
}
void display_rec(product p)
{
cout<<p.name<<" "<<p.no<<" "<<p.price<<endl;
}
void main()
{
        cout<<"program accesses the records already presenin            the disc"<<endl;
int rec_length,rec_no;
product temp[100];
rec_length=sizeof(temp);
fin.open("inventory",ios::in);
cout<<"enter the record number to be accessed...to quit enter -999"<<endl;
cin>>rec_no;
while(rec_no!=-999)
{
fin.seekg(rec_length*(rec_no-1),ios::beg);
read_rec(temp[rec_no]);
display_rec(temp[rec_no]);
cout<<"enter the record number to be accessed...to quit enter -999"<<endl;
}
fin.close();
}

line 11: you need to pass the structure by reference so that changes made in that function will be retained by the calling function. Do it like this (see the & operator) void read_rec(product& p) line 25: sizeof(temp) does not produce the size of the product structure but the number of bytes in the entire array. If you want the number of elements in the array than do it like this: rec_length = sizeof(temp) / sizeof(temp[0]); If the file is a text file then it can not be read like you are trying to do in that loop beginning on line 29. If the file is binary, then you have to specify ios::binary in the open statement on line 26: fin.open("inventory",ios::binary | ios::in);

line 11: you need to pass the structure by reference so that changes made in that function will be retained by the calling function. Do it like this (see the & operator) void read_rec(product& p) line 25: sizeof(temp) does not produce the size of the product structure but the number of bytes in the entire array. If you want the number of elements in the array than do it like this: rec_length = sizeof(temp) / sizeof(temp[0]); If the file is a text file then it can not be read like you are trying to do in that loop beginning on line 29. If the file is binary, then you have to specify ios::binary in the open statement on line 26: fin.open("inventory",ios::binary | ios::in);

the file is a text file....and tat s the problem...can we access text files randomly??and i m not making any changes in the function to get reflected in the main()...i hope i m correct...

>>can we access text files randomly

Short answer, no. Well, probably not. Depends on how the file is written. Er, I don't think so.

Since each object in the file contains an array of possibly indeterminant/nonstandard length, then the number of bytes of memory required for each objects' information storage in the file is different. If each object has a defined size (you could write the information to the file so each name takes up exactly 20 bytes by padding with spaces, etc), then you can randomly access the file by creating/calculating an offset. Or if you have some other way of calculating an offset, then you might be able to do random file access. Otherwise, read the entire file (or at least a portion) into the program storing it in a container that implements random access and access the information from within the container. At least that's my understanding. Always happy to learn new things as my "name" suggests.

>>i m not making any changes in the function to get reflected in the main()...

Based on the following lines I suspect you want to maintain the changes made to temp[rec_no] in read_rec() so you can display it in display_rec() and since both fucntion calls occur within main() you should follow Ancient Dragon's advice.

read_rec(temp[rec_no]);
display_rec(temp[rec_no]);

You can take a read-only text file (not exactly true, but it will save listing all the cases), and then you can produce an index of that file of all the 'tell' values for the start of each line.

You can then use those positions in later 'seek' calls to go directly to the start of a line in the file, and then read that line using getline.

>>i m not making any changes in the function to get reflected in the main()...

Based on the following lines I suspect you want to maintain the changes made to temp[rec_no] in read_rec() so you can display it in display_rec() and since both fucntion calls occur within main() you should follow Ancient Dragon's advice.

read_rec(temp[rec_no]);
display_rec(temp[rec_no]);

thank u for all the inputs....
i just have a small doubt now....will a file created in the program using open command gets listed in command line directory??
thanx in advance...

I'm not sure what you mean by the command line directory. Have your tried creating a file and see where it is found in your computer's directory? By using a path name rather than just a file name with the open() command you should be able to create the file and place it wherever you want in your computers directory.

I'm not sure what you mean by the command line directory. Have your tried creating a file and see where it is found in your computer's directory? By using a path name rather than just a file name with the open() command you should be able to create the file and place it wherever you want in your computers directory.

ok...thank u....

This article has been dead for over six months. Start a new discussion instead.