help with ofstream output programming!

This is an excerpt of my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if (save == 1)
                                   {
                                            for (save = 1; save <= record;save++)
                                            {
                                                cout << info[save].fname << endl;
                                                cout << info[save].lname << endl;
                                                cout << info[save].mail << endl;
                                                cout << info[save].phone << endl;
                                                cout << endl;
                                            }
                                            ofstream saving ("ContactList.txt");
                                            for (save = 1; save <= record; save++)
                                            {
                                                cout << info[save].fname << endl;
                                                cout << info[save].lname << endl;
                                                cout << info[save].mail << endl;
                                                cout << info[save].phone << endl;
                                                cout << endl;
                                            }
                                            cout << "\n\nSaved successfully\n";
                                            break;
                                   }



and this is the output for a typical run.


see
see
see
see

plus
plus
plus
plus

see
see
X%<
0


♦êD
░2D




Saved successfully



As you can see, the arrays was doing fine with the data i input earlier. but as soon as the program comes to the ofstream part, it just screws up everything.

any help would be much appreciated.

I'm using Dev C++ and my headers are <cstdlib>, <iostream> and <fstream>
Last edited on
As i can see from your code .. you are not writting any thing in the file .. you are displaying it in the cmd window .
yes, i am displaying it for the sake of showing that the data's somehow got corrupted. It will write the second part of the output inside the .txt file. This is my way of pinpointing which part of the code went wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (save == 1){
        for (save = 1; save <= record;save++){
                cout << info[save].fname << endl;
                cout << info[save].lname << endl;
                cout << info[save].mail << endl;
                cout << info[save].phone << endl;
                cout << endl;
        }
}
ofstream saving ("ContactList.txt",ios::out);
for (save = 1; save <= record; save++){
    saving << info[save].fname << endl;
    saving << info[save].lname << endl;
    saving << info[save].mail << endl;
    saving << info[save].phone << endl;
    saving << endl;
}
saving.close();
cout << "\n\nSaved successfully\n";
}
Last edited on
mind explaining your code?

I mean, i saw that you added saving.close(), but that doesn't change the fact that the arrays got screwed after ofstream saving.

Thanks
From code you've posted before, i suppose that 'info' is an array of struct with fields 'fname,lname,mail,phone' of 'record' elements. However, here the code that work for me:
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
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

#define LEN_MAX 50

typedef struct{
    char fname[LEN_MAX+1];
    char lname [LEN_MAX+1];
    char mail[LEN_MAX+1];
    char phone[LEN_MAX+1];
} Info;

int main()
{
    int record;
    cin>>record;
    Info info[record];
    for(int i=0;i<record;++i){
        cin.getline(info[i].fname,LEN_MAX);
        cin.getline(info[i].lname,LEN_MAX);
        cin.getline(info[i].mail,LEN_MAX);
        cin.getline(info[i].phone,LEN_MAX);
    }
    ofstream saving("ContactList.txt",ios::out);
    for(int i=0;i<record;++i){
        saving<<endl<<info[i].fname<<endl;
        saving<<info[i].lname<<endl;
        saving<<info[i].mail<<endl;
        saving<<info[i].phone<<endl;
    }
    saving.close();
    cout << "\n\nSaved Successfully\n";
    return 0;
}
Last edited on
hey, i tried adding a #define. and for the second part, the program finally worked. but the first part is still not working.

the output:
john
john
john
john

james
james
james
james




D

james
james
james
james



Saved successfully


If it helps, i can show you the whole code. but it's too long to be posted here

Edit: here you go. http://www.wepaste.com/seeplusplus/

Last edited on
i can't see it, require password
please check your pm inbox :)
Topic archived. No new replies allowed.