Jump to content

Making an .exe program store data as entered by the user

- - - - -

  • Please log in to reply
7 replies to this topic

#1
squid attack

squid attack

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
I am trying to find code that will allow my data-entry program retain the data entered into it by a user once the program has been converted to an exe and put on the computer desktop.
The problem is that I've scoured the net but am generally unfamiliar with the relevant jargon.
Several tutorials regarding fstream and binary fstream which I only THINK are what I need, have proved to be so user unfriendly that I just cannot understand them, ie;

Input/Output with files
fstream - C++ Reference

and daniweb and Learncpp.com. None of these seem to address my problem, although I did learn to
write to a file out of these tutorials, which was relatively straightfoward.

I just want to know if there is a tutorial that will show me the code I need to write into my program so that when it is converted to a exe the user can enter information and then later add more data and so on, even after the program has been turned off, in the same way as any normal data base program operates. The information needs to be stored. I wrote a data base program and each time the computer is turned off or the program turned off, the information entered is lost.

Thanks for anyone directing me to a site covering this, or taking the time to show me the relevant code.

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
It's pretty simple, as you've said already. Basicly you open file, write to it or read from it and that's what you need to do. You already wrote to it, just make your program like so that when it starts it loads (reads data from your file) data and then do whatever you want with it.

C programming - File I/O
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
squid attack

squid attack

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Thanks. In that link the code is;
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
  char str[10];
  ofstream a_file ( "example.txt" );
   a_file<<"This text will now be inside of example.txt";
  a_file.close();
  ifstream b_file ( "example.txt" );
  //Reads one string from the file
  b_file>> str;
  //Should output 'this'
  cout<< str <<"\n";
  cin.get();  
}

My question is why does it only retrieve the word 'this' from the string.ie; //Reads one string from the file.
Even if I put char str[100] or change it to a string other errors occur?
Why is the full data not being printed.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
You have to read from file in a loop and check wheter you've reached EOF (End Of File). Also, I think that >> operator reads to the first occurance of space or new line so I suggest you use getline.


#include <iostream>

#include <fstream>

#include <string>

using namespace std;


int main() {

    string line;

    ifstream file("file.txt");


    if (!file) {

        cout << "file not found\n";

        return 1;

    }


    while (!file.eof()) {  // read until eof is reached

        getline(file, line);

        cout << line;

    }


    return 0;

}


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
squid attack

squid attack

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Right. Why didn't they put that in the tutorial??
Also, does that mean that the code you showed, need to be written each and every time various functions are required to retrieve data from the file within my program?

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

squid attack said:

Right. Why didn't they put that in the tutorial??
Also, does that mean that the code you showed, need to be written each and every time various functions are required to retrieve data from the file within my program?
Sorry, but I'm afraid I don't understand your question.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

squid attack said:

Right. Why didn't they put that in the tutorial??
Ask the author.

Quote

Also, does that mean that the code you showed, need to be written each and every time various functions are required to retrieve data from the file within my program?
If you want to read lines with whitespace, yes. Personally I prefer to use fopen, it does not care about whitespace, I had never seen any software that I can recall that really uses anything else, although its preference I guess. C++ introduces classes I never use. You may as well though, they're somewhat safer for new programmers.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
squid attack

squid attack

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
No worries, thanks.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users