files confuse me (solved)

If i were to make a program, or have a piece of a program, write numbers to a file, and lets say I close the program. The program should print the numbers when someone starts a program.

So what i want the program to do is :

[program is opened, (by someone)]
[information, if any is there, is printed]
...code...
[numbers are written to a file]
[program is closed, (by someone)]

I've tried to do this before multiple times, but the only catch is trying to both write and read from a file in the same program


any help would be nice, thanks

[edit] the problem has been solved, thanks to Moschops for all that help, you rok d00d [/edit]





Last edited on
Explain more. Your post isn't quite lucid enough. I may be able to help if you elucidate a little bit.
both write and read from a file in the same program


Like this:

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

int main () {
  ofstream outputFile ("example.txt");
  if (outputFile.is_open())
  {
    outputFile << "This is a line.\n";
    outputFile << "This is another line.\n";
    outputFile.close();
  }
  else cout << "Unable to open file";

  string line;
  ifstream inputFile ("example.txt");
  if (inputFile.is_open())
  {
    while ( inputFile.good() )
    {
      getline (inputFile, line);
      cout << line << endl;
    }
    inputFile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}


which is covered here:
http://www.cplusplus.com/doc/tutorial/files/
Last edited on
Thanks moschops, that helped a ton, but one more thing, how can I print them separately, for example, if have the 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
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main () {

  int x = 43;
  int y = 51;

  char bufferx [33];
  char buffery [33];

  itoa (x,bufferx,10);
  itoa (y,buffery,10);

  ofstream outputFile ("example.txt");
  if (outputFile.is_open())
  {
    outputFile << bufferx << endl;
    outputFile << buffery << endl;
    outputFile.close();
  }
  else cout << "Unable to open file";

  string line;
  ifstream inputFile ("example.txt");
  if (inputFile.is_open())
  {
    while ( inputFile.good() )
    {
      getline (inputFile, line);
      cout << line << endl;
    }
    inputFile.close();
  }

  else cout << "Unable to open file";

  return 0;
}



and I wanted to individually print each buffer, how would I do that?
Last edited on
By writing
outputFile << bufferx << endl;
when you wanted to output bufferx and
outputFile << buffery << endl;
when you wanted to output buffery
sorry for pitching so many requests, but I do have another question (again, sorry)

this

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

int main () {

    int x;
    int y;

    x = 44;
    y = 51;

    char bufferx [33];
    char buffery [33];

    itoa (x,bufferx,10);
    itoa (y,buffery,10);

    ifstream inputFile ("example.txt");
    if (inputFile.is_open()){
        cout << bufferx << endl;
        cout << buffery << endl;
    }

    else cout << "Unable to open file";

    x = 47;
    y = 52;

    ofstream outputFile ("example.txt");
    if (outputFile.is_open())
    {
        outputFile << bufferx << endl;
        outputFile << buffery << endl;
        outputFile.close();
    }
    else cout << "Unable to open file";

    return 0;
    }


is the code I have now(sorry for it being so space consuming) , slightly edited it to make it print values before writing values to a file. There is one problem, from this code, as many of you may know, printing bufferx and buffery before they are declared is a bad thing, so I was wondering how I could separately print line one (which would be bufferx) and line two (which would be buffery) of the file before declaring x and y. Also, can the two values not be printed with a loop? thanks again, I appreciate all of your help!
have no fear, the problem has been solved

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

int main () {

    string line;
    string arr [99999];
    int ctr = 1;

    ifstream inputFile ("example.txt");
    if (inputFile.is_open()){
        while ( inputFile.good()){
            getline (inputFile,line);
            arr[ctr] = line;
            cout << arr[ctr] << endl;
            ctr = ctr + 1;
        }
    }
    else cout << "Unable to open file";

    int x = 0;
    int y = 0;

    x = 44;
    y = 51;

    char bufferx [33];
    char buffery [33];

    itoa (x,bufferx,10);
    itoa (y,buffery,10);

    x = 47;
    y = 52;

    ofstream outputFile ("example.txt");
    if (outputFile.is_open())
    {
        outputFile << bufferx << endl;
        outputFile << buffery << endl;
        outputFile.close();
    }
    else cout << "Unable to open file";

    //outputFile << bufferx << endl;
    //outputFile << buffery << endl;

    return 0;
    }



its a bit sloppy, but it fills the basic need
Topic archived. No new replies allowed.