Getting going in the right direction

Looks for maybe a few pointers where I could figure out a few things

Page 1 of 1

10 Replies - 547 Views - Last Post: 15 June 2009 - 01:18 PM Rate Topic: -----

#1 CryptiC-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-June 09

Getting going in the right direction

Post icon  Posted 15 June 2009 - 05:20 AM

Ok for starters, great site been looking around and reading alot, extremely helpful.

Now i'm still new to C++, but i've been starting to program a inventory console application in windows, and I just came to the point to where I want to be able to have files holding my information of products.

Like say I can have a menu to let me put in when I get more stock, then when I make a sale it can do all its math and whatnot.

The issue im having is figuring out how I need to write up the code, I got code that will create the file(I just made it "inventory.txt"), But I cannot find a tutorial on making previous stuff stay in files and just add on to what I had before. I know its something simple and right under my nose

I have been searching for like 48 hours and decided to finally ask the community.

Im not asking for code merely just a good shove in the right direction.

Thanks in advance,
CryptiC-

Is This A Good Question/Topic? 0
  • +

Replies To: Getting going in the right direction

#2 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Getting going in the right direction

Posted 15 June 2009 - 05:29 AM

FILE* file;
  file = fopen ("myfile.txt","a+");
 

more

a+ means append and readable

a means append only




yay
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Getting going in the right direction

Posted 15 June 2009 - 08:00 AM

View PostCryptiC-, on 15 Jun, 2009 - 04:20 AM, said:

Ok for starters, great site been looking around and reading alot, extremely helpful.

Now i'm still new to C++, but i've been starting to program a inventory console application in windows, and I just came to the point to where I want to be able to have files holding my information of products.

Like say I can have a menu to let me put in when I get more stock, then when I make a sale it can do all its math and whatnot.

The issue im having is figuring out how I need to write up the code, I got code that will create the file(I just made it "inventory.txt"), But I cannot find a tutorial on making previous stuff stay in files and just add on to what I had before. I know its something simple and right under my nose

I have been searching for like 48 hours and decided to finally ask the community.

Im not asking for code merely just a good shove in the right direction.

Thanks in advance,
CryptiC-


you need to set your flags for you file, so say:
ofstream FILE("inventory.txt",ios::app);



ios::app = All output operations are performed at the end of the file, appending the content to the current content of the file.
Was This Post Helpful? 0
  • +
  • -

#4 AGRAC393  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 248
  • Joined: 13-May 09

Re: Getting going in the right direction

Posted 15 June 2009 - 09:19 AM

Your in the right place!!
Was This Post Helpful? 0
  • +
  • -

#5 CryptiC-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-June 09

Re: Getting going in the right direction

Posted 15 June 2009 - 10:31 AM

Thanks so much for the help so far guys I didnt have much time to mess around with it but I will be when I get off work today.

Again sorry if the post was covered a million times, my search efforts seemed fruitless, just looks like I wasn't looking around very well lol

Any other pages on file i/o or maybe a good reference sheet to print out?

When I come home from work and mess with it ill post my code up on here and let you guys take a look.. Im sure im doing so many wrong things or not very optimized so hopefully I can get more pointers lol
Was This Post Helpful? 0
  • +
  • -

#6 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Getting going in the right direction

Posted 15 June 2009 - 10:39 AM

There are two main ways for io, cstdio.h and fstream.

in my opinion, cstdio is good because it has functions that manipulate strings more carefully in detail. but fstream is good because it's fast to use with >> and << like

file << string;


would write a string straight into the txt without much headache.
Was This Post Helpful? 0
  • +
  • -

#7 CryptiC-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-June 09

Re: Getting going in the right direction

Posted 15 June 2009 - 10:42 AM

View PostKanvus, on 15 Jun, 2009 - 09:39 AM, said:

There are two main ways for io, cstdio.h and fstream.

in my opinion, cstdio is good because it has functions that manipulate strings more carefully in detail. but fstream is good because it's fast to use with >> and << like

file << string;


would write a string straight into the txt without much headache.


Thats another thing im gonna wanna be able to input a string for a name and some numbers per each entry into this file. I have to go do some reading on strings its sounding like haha
Was This Post Helpful? 0
  • +
  • -

#8 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Getting going in the right direction

Posted 15 June 2009 - 10:55 AM

it's really simple and looking at examples would express the idea.

say file was opened as whatsfordinnermom.txt and only has one line,
nothing




#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(){
	
	string hi;
	
	fstream file;
	file.open("whatsfordinnermom.txt");
	
	file >> hi; // hi now has "nothing"
	cout << hi; // show hi to screen
	
	cout << endl;
	system("PAUSE");
	return 0;   
}



everytime you repeat those two lines, it would skip to the next line in the whatsfordinnermom.txt file so you don't have to worry about moving the iterator forward.

This post has been edited by Kanvus: 15 June 2009 - 10:57 AM

Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Getting going in the right direction

Posted 15 June 2009 - 11:01 AM

dont for get your ios::app to appent your text file, and to close your file

View PostKanvus, on 15 Jun, 2009 - 09:55 AM, said:

it's really simple and looking at examples would express the idea.

say file was opened as whatsfordinnermom.txt and only has one line,
nothing




#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(){
	
	string hi;
	
	fstream file;
	file.open("whatsfordinnermom.txt",ios::app);
	
	file >> hi; // hi now has "nothing"
	file.close();

	cout << hi; // show hi to screen
	
	cout << endl;
	cin.get();
	return 0;   
}



everytime you repeat those two lines, it would skip to the next line in the whatsfordinnermom.txt file so you don't have to worry about moving the iterator forward.

This post has been edited by ImaSexy: 15 June 2009 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

#10 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Getting going in the right direction

Posted 15 June 2009 - 11:10 AM

the problem is ios::app makes the iterator start at the end so hi would receive nothing in this case. but when actually coding, yes, remember the app along with other rules like out only or in only.

file.open("whatsfordinnermom.txt",ios::app | ios::out); 



that would only allow output and apply output to the end. imo i would use cstdio for the "a+" from above.
Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Getting going in the right direction

Posted 15 June 2009 - 01:18 PM

View PostKanvus, on 15 Jun, 2009 - 10:10 AM, said:

the problem is ios::app makes the iterator start at the end so hi would receive nothing in this case. but when actually coding, yes, remember the app along with other rules like out only or in only.

file.open("whatsfordinnermom.txt",ios::app | ios::out); 



that would only allow output and apply output to the end. imo i would use cstdio for the "a+" from above.


but isnt that what he is trying to do?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1