Storing Information in a File

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1347
  • View blog
  • Posts: 4,642
  • Joined: 19-February 09

Re: Storing Information in a File

Posted 25 November 2012 - 08:29 PM

Did you test the stream as I asked?
Was This Post Helpful? 1
  • +
  • -

#17 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:42 PM

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

Did you test the stream as I asked?

Yeah, I had basically the same code for the readFile, and nothing was created, nor did I get an error...The program the way that it is coded basically is set up to except any type of file whether it exists or not. Not sure how i would actually get an error based on that any way.
Was This Post Helpful? 0
  • +
  • -

#18 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1347
  • View blog
  • Posts: 4,642
  • Joined: 19-February 09

Re: Storing Information in a File

Posted 25 November 2012 - 09:23 PM

I think you said that the file was being truncated earlier.

You could try this again :

  WriteToFile.open(FileName);



and also try

  WriteToFile.open(FileName, fstream::out | fstream::app);




If you show your current code we could see if it is something small.
Was This Post Helpful? 1
  • +
  • -

#19 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 - 10:06 PM

View Post#define, on 25 November 2012 - 09:23 PM, said:

I think you said that the file was being truncated earlier.

You could try this again :

  WriteToFile.open(FileName);



and also try

  WriteToFile.open(FileName, fstream::out | fstream::app);




If you show your current code we could see if it is something small.


Here is the code I have currently:

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.c_str(), fstream::app);
	
		if( !WriteToFile.good()) 
		cout << "WriteToFile bad" << endl;

	WriteToFile << UserInput << endl;
	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);
			cout << " " << ReadBuffer << endl;
		}
			ReadFromFile.close();
	}	
	else
		cout << "Failed to open the file \n";
	system("pause");
	return 0;
}


Yes it did truncate on my first attempt so yeah, I could go back to the previous code. The only problem is that it won't read from the file so it basically takes me back to where I started.
Was This Post Helpful? 0
  • +
  • -

#20 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1347
  • View blog
  • Posts: 4,642
  • Joined: 19-February 09

Re: Storing Information in a File

Posted 25 November 2012 - 11:09 PM

View Postneewb, on 26 November 2012 - 07:06 AM, said:

Yes it did truncate on my first attempt so yeah, I could go back to the previous code. The only problem is that it won't read from the file so it basically takes me back to where I started.


If you revert from a code that works you could try some different code. If it works when there is no mode then it is likely something to do with mode. Perhaps someone with Visual Studio can try running it.

You could try creating a program to write to a file.

Another variation :-

  WriteToFile.open(FileName.c_str(), ios::out | ios::app);




You might not be able to write to a root directory.

To see errors you may need

23       //system("CLS");


Was This Post Helpful? 1
  • +
  • -

#21 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:31 PM

View Post#define, on 25 November 2012 - 11:09 PM, said:

View Postneewb, on 26 November 2012 - 07:06 AM, said:

Yes it did truncate on my first attempt so yeah, I could go back to the previous code. The only problem is that it won't read from the file so it basically takes me back to where I started.


If you revert from a code that works you could try some different code. If it works when there is no mode then it is likely something to do with mode. Perhaps someone with Visual Studio can try running it.

You could try creating a program to write to a file.

Another variation :-

  WriteToFile.open(FileName.c_str(), ios::out | ios::app);




You might not be able to write to a root directory.

To see errors you may need

23       //system("CLS");



"You could try creating a program to write to a file."

That is exactly what I am going to be doing, but before i can create this program I need to understand and create a file where information will be stored into, see the very beginning of this post (I have the program instructions from my first post.

I have tried many different variations of creating files, and none seem to work, except for the one that I posted that you have seen already. Another one that I tried was having the file be created without user intervention. This was taken right straight out from my book:

int main()
 {
 // Buffer used for reading.
 Invtry record;

 // Create and open the file for reading.
 fstream inventory("invtry.dat", ios::in | ios::binary);
 if (!inventory)
 {
 cout << "Error in opening the file.";
 return 0;
 }

 // Now read and display the records
 inventory.read(reinterpret_cast<char *>(&record),
 sizeof(record));
 while (!inventory.eof())
 {
 cout << "Description: ";
 cout << record.desc << endl;
 cout << "Quantity: ";
 cout << record.qty << endl;
 cout << "Price: ";
 cout << record.price << endl << endl;
 inventory.read(reinterpret_cast<char *>(&record),
 sizeof(record));
 }
 inventory.close();
 return 0;
 }


I tried this code,and it did nothing but display the cout statements on the screen, and that's it. No file was created like it was supposed to. My program that I am to make, is very similar to the above code, but I need the file part to work before I can even start this program, because the whole idea of the program is to be able to read and write to a file, and have it be saved there....
Was This Post Helpful? 0
  • +
  • -

#22 jimblumberg  Icon User is offline

  • member icon


Reputation: 4075
  • View blog
  • Posts: 12,578
  • Joined: 25-December 09

Re: Storing Information in a File

Posted 26 November 2012 - 07:25 AM

Have you tried using an ofstream() to verify that your problem is not a file system issue? The ofstream() parameters will truncate (erase) your file so make sure you use a different name for your file. Now if the following program succeeds you should use your file system find functionality to locate this file and then place your input file in the same directory. If the program doesn't succeed you need to run the program from the command line so you can see the operating system error messages for hints as to why the file opening failed.

#inlcude <iostream>
#include <fstream>

int main()
{
   // Create and open the file for reading.

   ofstream inventory("YourInventory.dat");
   if (!inventory)
   {
      cerr << "Error in opening the file.";
      return 1;
   }

   return 0;
}




Jim
Was This Post Helpful? 1
  • +
  • -

#23 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 26 November 2012 - 12:02 PM

View Postjimblumberg, on 26 November 2012 - 07:25 AM, said:

Have you tried using an ofstream() to verify that your problem is not a file system issue? The ofstream() parameters will truncate (erase) your file so make sure you use a different name for your file. Now if the following program succeeds you should use your file system find functionality to locate this file and then place your input file in the same directory. If the program doesn't succeed you need to run the program from the command line so you can see the operating system error messages for hints as to why the file opening failed.

#inlcude <iostream>
#include <fstream>

int main()
{
   // Create and open the file for reading.

   ofstream inventory("YourInventory.dat");
   if (!inventory)
   {
      cerr << "Error in opening the file.";
      return 1;
   }

   return 0;
}




Jim


Thanks Jim, that was the problem. I wasn't running the program from the command line, I was running it from my virtual sandbox, and had not realized it till late last night. That was the reason, the files were not created, everything is good now, thanks again...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2