Some problems

Hello everybody, to start explaining I first of all need to tell you I’m quite new in this field and I don’t know what is wrong with my code...
I hope this is not a problem with my compiler (Code::Blocks) because it would be a drag to have to reinstall it again(recently I had some problems with my computer but that is not the case anymore), but the next samples I want to attach do not work, because the system doesn’t want to open the files, and I can’t find any problems with them (then again I only started C++ 2 month ago), so please tell me if there is anything wrong with my syntax and I will be very grateful to you. Thank you in anticipation.

The first one(more recent):

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
  #include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;

int get_int(int default_value);
char name[20];

int main(){
    char filename[81];
    int n;
    int age;
    int recsize = sizeof(name) + sizeof(int);

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

    

    fstream fbin(filename, ios::binary | ios::in | ios::out);

    if(!fbin){
        cout<<"File " << filename << " could not be opened.";
        return -1;
    };

    cout << "Enter file record number: ";
    n = get_int(0);

    cout << "Enter name: ";
    cin.getline(name, 19);
    cout << "Enter age: ";
    age = get_int(0);

    fbin.seekp(n * recsize);
    fbin.write(name, 20);
    fbin.write(reinterpret_cast<char*>(&age), sizeof(int));
    fbin.close();
    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);
}


The second one:

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
#include <iostream>
#include <fstream>
using namespace std;

int main(){
    int c, i;

    char filename[81];
    char input_line[81];
    cout << "Enter a file name and press ENTER: ";
    cin.getline(filename, 80);

    ifstream file_in(filename);

    if(!file_in){
        cout << "File " << filename << " could not be opened.";
        return -1;
    }

    while(1){
        for(i = 1; i <= 24 && ! file_in.eof(); i++){
            file_in.getline(input_line, 80);
            cout << input_line << endl;
        }
        if(file_in.eof())
            break;
        cout << "More? (press 'Q' and ENTER to quit)"<< endl;
        cin.getline(input_line, 80);
        c  = input_line[0];
        if(c == 'Q' || c == 'q')
            break;

    }
    return 0;
}
Last edited on
Please show an example of the file name.

Also be aware that with both of your programs the file must exist. With the open modes you're currently using the file won't be created if it doesn't exist.

You may want to have the program tell you a little more information about why the file doesn't open by using the perror() function:

1
2
// In your file open check:
   perror ("The following error occurred ");


Be sure to include <cstdio>

Thank you very much, yes the perror function said that the file does not exit, I was using a directory, I didn't knew that is a mistake =)
Topic archived. No new replies allowed.