Storing Information in a File

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1280 Views - Last Post: 26 November 2012 - 12:02 PM Rate Topic: -----

#1 neewb  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Storing Information in a File

Posted 25 November 2012 - 10:42 AM

I am very confused with this assignment:

Write a program that uses a structure to store the following information in a file:

Item Description
Quantity On Hand
Wholesale Cost
Retail Cost
Date added to to inventory


Then the program should have a menu that allows the user to perform these tasks:

Add new records to the file
Display any record in the file
Change any record in the file


My confusion is with the file itself. Just what exactly are you supposed to write in the file? Can you create this file while coding the program? Or do you have to have some preset information inside it? For example: If I wanted to access a file named inventory.dat, would I need to have information inside it, or can this file be created while the program is getting coded?

So now in relation to the program that I need to write, Do I put the item descriptions that I mentioned above inside the file before writing the program? Or can they just be put inside the code and then the file can be modified by the user and then saved to this file? Very confused about files here....

Is This A Good Question/Topic? 0
  • +

Replies To: Storing Information in a File

#2 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Storing Information in a File

Posted 25 November 2012 - 10:56 AM

Based on what you have written, I'm assuming you are new to the concept of file I/O. So, here I try to give you some starting information:

Basically, when you need to work with files, you open a connection to this file by giving the name/address to the corresponding function/class. During this phase, you specify the type of this connection: are you going to do only input? Or only output? Or maybe both of them.

Then, you have to check the state and see what just happened? Is this connection opened successfully? If yes, you can proceed.

Now comes the most important part: where you actually write to or read from the file. The C++ way of file I/O is different than that of C. So, I can't hint you more in it as you haven't specified which language you are working with.

Edit: by connection, I didn't mean any network connections; so don't confuse with that. What I had in mind was a connection between your program and the file on hard disk.

This post has been edited by Anarion: 25 November 2012 - 10:58 AM

Was This Post Helpful? 1
  • +
  • -

#3 neewb  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Re: Storing Information in a File

Posted 25 November 2012 - 11:21 AM

View PostAnarion, on 25 November 2012 - 10:56 AM, said:

Based on what you have written, I'm assuming you are new to the concept of file I/O. So, here I try to give you some starting information:

Basically, when you need to work with files, you open a connection to this file by giving the name/address to the corresponding function/class. During this phase, you specify the type of this connection: are you going to do only input? Or only output? Or maybe both of them.

Then, you have to check the state and see what just happened? Is this connection opened successfully? If yes, you can proceed.

Now comes the most important part: where you actually write to or read from the file. The C++ way of file I/O is different than that of C. So, I can't hint you more in it as you haven't specified which language you are working with.

Edit: by connection, I didn't mean any network connections; so don't confuse with that. What I had in mind was a connection between your program and the file on hard disk.


Thanks for replying, this is a C++ program,and yes I am new to working with files in my programs. It is for both input and output. Basically the file needs to be input for viewing or reading what is in it, and output for writing to the file. I just don't understand how to actually start creating this file, and what is supposed to go in it. I am thinking though that whatever the user enters into the program should be saved to this file, but other than I have no clue...
Was This Post Helpful? 0
  • +
  • -

#4 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Storing Information in a File

Posted 25 November 2012 - 11:29 AM

OK then, you have to work with a file stream. You should check your book/the internet for "file stream" or the abbreviated name of "fstream". I would guide you if you had a specific problem with some part of file streams, but as the problem is the whole thing, I don't think I can be a better guide than a book :)

Also, I would suggest you check out this nice tutorial after you read some on the subject.
Was This Post Helpful? 1
  • +
  • -

#5 neewb  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Re: Storing Information in a File

Posted 25 November 2012 - 04:32 PM

View PostAnarion, on 25 November 2012 - 11:29 AM, said:

OK then, you have to work with a file stream. You should check your book/the internet for "file stream" or the abbreviated name of "fstream". I would guide you if you had a specific problem with some part of file streams, but as the problem is the whole thing, I don't think I can be a better guide than a book :)/>

Also, I would suggest you check out this nice tutorial after you read some on the subject.


Ok, so I am learning about files. I have viewed some tutorials online and notice that you can create files that do not not even exist yet, as C++ will automatically create one for you. The problem is that when I go to create a new file, I cannot locate it anywhere on my drive. I named it someFile.txt and it is nowhere to be seen. I put some comments in it and saved it to that file, but yet cannot locate it on the drive. Is there something I need to do in my Visual compiler settings so that it can locate the file?
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,512
  • Joined: 19-February 09

Re: Storing Information in a File

Posted 25 November 2012 - 05:08 PM

You could try search from the start menu (search is also present in an explorer window).

Have you saved a project somewhere?
Was This Post Helpful? 1
  • +
  • -

#7 neewb  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Re: Storing Information in a File

Posted 25 November 2012 - 05:23 PM

View Post#define, on 25 November 2012 - 05:08 PM, said:

You could try search from the start menu (search is also present in an explorer window).

Have you saved a project somewhere?

Yeah I was finally able to create the file. However; I can only write to it not read the contents inside it. The only way I can read the file is if I go into the directory to where it is located at and open the text file from there, not by reading it from the output screen on Visual. Here is the code that I used, to at least create a file..

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
	string FileName,
			ReadBuffer;
	char UserInput[512];

	cout << "Please enter the name of the file to write to. \n";
	cout << "File Name:";
	cin >> FileName;
	cin.get();
	cout << "Text:";
	cin.getline(UserInput, 512);

	ofstream WriteToFile;
	WriteToFile.open(FileName);
	WriteToFile << UserInput;
	WriteToFile.close();

	system("CLS");
	cout << "Opening the file:" << FileName << " , the contents of the file are: \n\n";
	ifstream ReadFromFile(FileName);
	
	if (ReadFromFile.is_open())
	{
		while (!ReadFromFile.eof())
		{
			getline(ReadFromFile, ReadBuffer);
		}
			ReadFromFile.close();
	}	
	else
		cout << "Failed to open the file \n";
	return 0;
}

	



I want to at least understand how files get created, read and written to in order to work on this assignment.
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,512
  • Joined: 19-February 09

Re: Storing Information in a File

Posted 25 November 2012 - 05:46 PM

It looks ok, here you store the line in a string. I would try printing the string.

32      getline(ReadFromFile, ReadBuffer);
        cout << "out - " << ReadBuffer << endl;
33    }


This post has been edited by #define: 25 November 2012 - 05:46 PM

Was This Post Helpful? 1
  • +
  • -

#9 neewb  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Re: Storing Information in a File

Posted 25 November 2012 - 06:03 PM

View Post#define, on 25 November 2012 - 05:46 PM, said:

It looks ok, here you store the line in a string. I would try printing the string.

32      getline(ReadFromFile, ReadBuffer);
        cout << "out - " << ReadBuffer << endl;
33    }



Ok I tried that, but it doesn't show what is inside the file currently (if I had stored anything in there, shouldn't it display what is inside the file?) it only shows the string that was created inside the program not the whole file's contents... Also, I notice that whenever I add something to the text file, it will write over what was previously in it. So instead of displaying what I had in the file before, it gets erased and only shows what I just entered in...
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,512
  • Joined: 19-February 09

Re: Storing Information in a File

Posted 25 November 2012 - 06:31 PM

The default mode is to truncate the file (delete the data). You can try append mode :-

  WriteToFile.open(FileName.c_str(), fstream::app);



For other users : I've added .c_str() to the filename - newer compilers (Visual Studio 2010?) can open from C++ strings.
Was This Post Helpful? 1
  • +
  • -

#11 neewb  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Re: Storing Information in a File

Posted 25 November 2012 - 06:52 PM

View Post#define, on 25 November 2012 - 06:31 PM, said:

The default mode is to truncate the file (delete the data). You can try append mode :-

  WriteToFile.open(FileName.c_str(), fstream::app);



For other users : I've added .c_str() to the filename - newer compilers (Visual Studio 2010?) can open from C++ strings.


Ok, making progress, that worked for viewing, but when I went to add in some text from the screen, it would display it, but not save the new text in the file...I will only be able to see what was stored in there previously, not any new text that was added from the user.
Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,512
  • Joined: 19-February 09

Re: Storing Information in a File

Posted 25 November 2012 - 07:14 PM

View Postneewb, on 26 November 2012 - 03:52 AM, said:

Ok, making progress, that worked for viewing, but when I went to add in some text from the screen, it would display it, but not save the new text in the file...I will only be able to see what was stored in there previously, not any new text that was added from the user.


That's strange because what is being printed is coming from the file.

You could test whether the write file is opening properly.

You may want to add a newline at the end of the lines.

21	    WriteToFile << UserInput << endl;



And you could post the latest code if needed.
Was This Post Helpful? 1
  • +
  • -

#13 neewb  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Re: Storing Information in a File

Posted 25 November 2012 - 07:31 PM

View Post#define, on 25 November 2012 - 07:14 PM, said:

View Postneewb, on 26 November 2012 - 03:52 AM, said:

Ok, making progress, that worked for viewing, but when I went to add in some text from the screen, it would display it, but not save the new text in the file...I will only be able to see what was stored in there previously, not any new text that was added from the user.


That's strange because what is being printed is coming from the file.

You could test whether the write file is opening properly.

You may want to add a newline at the end of the lines.

21	    WriteToFile << UserInput << endl;



And you could post the latest code if needed.


Yes, it is printing whatever is currently in the file correctly. The problem is when I go to add any new text from Visual, it will display whatever I typed there, but when I open the text file it will not save any new information, only the previous content. It also seems that you cannot add any new files anymore either when it asks the user for a file name, that new file that gets created in Visual does not show up anywhere on my drive, this sure is getting frustrating....
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,512
  • Joined: 19-February 09

Re: Storing Information in a File

Posted 25 November 2012 - 07:59 PM

It sounds like a problem with the output file stream. Thats why I suggested testing it.

  WriteToFile.open(FileName.c_str(), fstream::app);
  if( !WriteToFile.good()) 
    cout << "WriteToFile bad" << endl;




Does file.txt work as a file name?
Was This Post Helpful? 1
  • +
  • -

#15 neewb  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Re: Storing Information in a File

Posted 25 November 2012 - 08:18 PM

View Post#define, on 25 November 2012 - 07:59 PM, said:

It sounds like a problem with the output file stream. Thats why I suggested testing it.

  WriteToFile.open(FileName.c_str(), fstream::app);
  if( !WriteToFile.good()) 
    cout << "WriteToFile bad" << endl;




Does file.txt work as a file name?


No, not one new file will work if entered in the program. I tried others as well and nothing. I don't like working with files...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2