collapse collapse

* Who's Online

  • Dot Guests: 116
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11164
  • stats Total Topics: 1700
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 1470

Author Topic: Printing data to a text file  (Read 3887 times)

0 Members and 1 Guest are viewing this topic.

Offline Devilddog2112

  • Newbie
  • *
  • Posts: 18
  • Reputation 25
    • View Profile
Printing data to a text file
« on: March 31, 2009, 07:09:01 am »
Code: [Select]
#include <fstream>
using namespace std;

int main()
{
ifstream inData;
ofstream outData;
 
char charLetter; // will be the ASCII letter that will be printed out
int intFirst = 69; // will be the first number that is entered in the program
int intSecond = 33; // will be the second number entered into the program

    inData.open("prog.dat");
outData.open("prog.out");

inData >> charLetter;
inData >> intFirst;
inData >> intSecond;

outData << "The ASCII value of your character: " << charLetter << "is" << int(charLetter) << endl;
outData << "The sum of " << 69 << " and " << 33 << " = " << 69 + 33 << endl;
outData << "The anwser of " << 69 << " and " << 33 << " = " << 69 * 33 << endl;



inData.close();
outData.close();
return 0;
}
can you please point me in the right direction, i have opened at .txt file in the resource file in Microsoft Visual 2008, but when i open the file in my folder the .txt file is blank

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: Printing data to a text file
« Reply #1 on: March 31, 2009, 07:36:41 am »
Well, your not even opening a txt file, and I do not believe if you make something a resource in visual studio it will be available to the program. I could be wrong, but I will have to try it.
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline Devilddog2112

  • Newbie
  • *
  • Posts: 18
  • Reputation 25
    • View Profile
Re: Printing data to a text file
« Reply #2 on: March 31, 2009, 07:40:48 am »
I am trying to get my code to print out the info on the text file.

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: Printing data to a text file
« Reply #3 on: March 31, 2009, 08:00:07 am »
I am trying to get my code to print out the info on the text file.
Output to prog.out right?

And can you please post the sample data?
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Printing data to a text file
« Reply #4 on: March 31, 2009, 02:04:21 pm »
Instead of using a text file, you could just use a string table to hold all your values. Here is a example with the project attached.
Code: [Select]
#include <iostream>
#include <windows.h>
#include "resource.h"
using namespace std;

int main()
{
char cVar[2];
char iVar[3];
char iVar2[3];

LoadString(NULL, FIRSTCHAR, cVar, sizeof(cVar));
LoadString(NULL, FIRSTNUM, iVar, sizeof(iVar));
LoadString(NULL, SECONDNUM, iVar2, sizeof(iVar2));

cout << cVar << endl;
cout << iVar << endl;
cout << iVar2 << endl;

int a = atoi(iVar);
int b = atoi(iVar2);

cout << a + b << endl;


return 0;
}

Its not commented because im in class, but basically, you load the 'id' of the string table slot you need. In my case, I named them FIRSTNUM, SECONDNUM and FIRSTCHAR. They are #defined to be 103, 104, and 105. So you can replace it with those and it will still work. Anyhow, load it, then use them as you need. IF you need the integer, use the array to integer function (atoi).
« Last Edit: March 31, 2009, 02:09:52 pm by Celestialkey »
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Devilddog2112

  • Newbie
  • *
  • Posts: 18
  • Reputation 25
    • View Profile
Re: Printing data to a text file
« Reply #5 on: March 31, 2009, 09:27:40 pm »
thank u,
for all your help all i did was rename the prog.out to .txt and it printed out to the .txt

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Printing data to a text file
« Reply #6 on: April 01, 2009, 05:40:33 am »
For the code above, even without the .txt, it printed out the data to a .out extension file. At least it did for me.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Devilddog2112

  • Newbie
  • *
  • Posts: 18
  • Reputation 25
    • View Profile
Re: Printing data to a text file
« Reply #7 on: April 01, 2009, 04:33:31 pm »
For the code above, even without the .txt, it printed out the data to a .out extension file. At least it did for me.
Hey brother,
thanks for your help, however i could not open the prog.out file.. until i changed the prog to txt file

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Printing data to a text file
« Reply #8 on: April 01, 2009, 05:03:30 pm »
Open by opening notepad first, then go to file -> open -> prog.out

Or just double click on it, go to Open file with program, then select notepad.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

 

Donate


* Search


* Recent Posts

I miss the chatbox by Shishka
[August 27, 2019, 11:52:39 pm]


Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]