Problems atrunnign the program.

I made everything according to the instructions, but it doesent read my file. Iwe tryed to figure out whats wrong but i cant find the error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>
using namespace std;

int get_int(int default_value);

int main(){
char filename[MAX_PATH +1];
int n =0;
char name[20];
char make[20];
int year = 0;
int recsize = sizeof(name) + sizeof(make) + sizeof(int);

cout << "Enter file name: ";
cin.getline(filename, MAX_PATH);

fstream fbin(filename, ios::binary | ios::in);
if(!fbin)
{
cout <<"Could not open " << filename << endl;
system("PAUSE");
return -1;
}

while(true)
 {
cout << "Enter file record number: ";
n = get_int(0);
if(n == -1) break;
fbin.seekp(n * recsize);

fbin.read(name, sizeof(name)-1);
fbin.read(make, sizeof(make)-1);
fbin.read((char*)(&year), sizeof(int));

cout << "The model is: " << name << endl;
cout << "Thae make is: " << make << endl;
cout <<" The year is: " << year << endl;
}
fbin.close();
system("PAUSE");
return 0;
}

int get_int(int default_value)
{
char s[81];

cin.getline(s, 80);
if(strlen(s) == 0) return default_value;
return atoi(s);
}
That code has no problem at all opening a file when I run it. My guess is that the file is not in the location you are entering.
my file is in the same folder as the proggram. the text file is written with this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>
using namespace std;

int get_int(int default_value);

int main ()
{
char filename[MAX_PATH +1];
int n = 0;
char name[20];
char make[20];
int year = 0;
int recsize = sizeof(name)+ sizeof(make) + sizeof(int);

cout << "enter file name: ";
cin.getline(filename, MAX_PATH);

fstream fbin(filename, ios::binary | ios::out);
if( !fbin)
{
cout << "Could not open " << filename << endl;
system("PAUSE");
return -1;
}
while(true)
{
cout << "Enter file record number: ";
n = get_int(0);
if(n == -1) break;

cout << "Enter model: ";
cin.getline(name, sizeof(name)-1);
cout << "Enter make: ";
cin.getline(make, sizeof(make)-1);
cout << "Enter year: ";
year = get_int(0);

fbin.seekp(n * recsize);
fbin.write(name, sizeof(name) -1);
fbin.write(make, sizeof(make)-1);
fbin.write((char*)(&year), sizeof(int));
}
fbin.close();
system("PAUSE");
return 0;
}

#define COL_WIDTH 80

int get_int(int default_value) {
char s[COL_WIDTH +1];
cin.getline(s, COL_WIDTH);
if (strlen(s) == 0)
return default_value;
return atoi(s);
}
Last edited on
And is the program's working directory the directory containing the text file? Some IDEs change the working directory.

What if you give the absolute path to the file, including the full directory location? Your code to open the file does work, so the problem must be that the file does not exist or the program is looking in the wrong place.
i gave it the full path to the text file, but i stil keep getting the same jibberish. I entered name is audi, make is plane oslt, and year sis 1999, but when i read it i get name: (hearth symbol), make: (empty) , year: 0. it like it cant read it or its reading something else.
Last edited on
no one knows how to help me ????
If you're reading a text file, should you really be stating ios::binary ? That makes it clear that the input is not to be treated as text.
Last edited on
well if you look at the writer program then you see that it writes a binary file. but my binary reader cant read it.
could somone run my writer program and write something and the usem y reader program to read it. DDo you get the same problem?
Last edited on
I used the example, i copyed it exactly like it was in th instructions, and i rund it, andi still got the same result. I wrote marko and 17, but i when i read the file wit other program it read that name is hearth symbol and age is 0. Coludl this be somkidn ov Devc++ bug???
Last edited on
No one??
Topic archived. No new replies allowed.