TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JABO
from: KURT KUZBA
date: 1998-03-08 04:47:00
subject: File streams in CPP

J>   and i cant add # to a string.
J>   Is there a way to convert a string -> int?
J>   or read a file as int?
J>   instad of fgets(); they need a fgetd();  B)
   These are really C questions, not C++.
   You should see if you can get the C_ECHO.
   In C++ you would use streams instead of fprintf() or fgets().
   Since I'm stream ignorant at the moment, though I shouldn't
   be, may I suggest using fread() and fwrite()?
   fwrite(&var, sizeof(var), 1, FilePointer);
   fread(&var, sizeof(var), 1, FilePointer);
   Also, you should be aware of the ANSI standard atoi() and
   itoa() functions for conversion of ascii to int and int to
   ascii. check the function prototypes in stdlib.h for a list
   of such useful functions and the arguments they require.
   If you have an online help feature, look them up in there,
   too.
   I found this in my archives, and it works well enough.
   I don't think it answers all your questions, but it is a
   starting place. It was posted by Cliff Rhodes long ago. :)
   I made one or two slight changes where my compiler would
   not accept a cast to char* in the infile.read() function.
#include 
#include 
#include 
struct Person {
  char name[36];
  int  age;
  int  weight;
};
int main(void)
{
  const char *fname = "PERSON.DAT";  // Filename we'll use
  Person aPerson;  // Create a person
  // Give the person an identity...
  strcpy(aPerson.name, "Joe Blow");
  aPerson.age = 21;
  aPerson.weight = 175;
  ofstream outfile(fname, ios::binary); // Open the file for writing
  if(!outfile)
  {
     cerr << "Couldn't open data file for writing" << endl;
     return 1;
  }
  // Write the struct to the opened file
  // Notice the cast of &aPerson to const char *
  outfile.write((const char *) &aPerson, sizeof(Person));
  outfile.close(); // Close the stream
  Person bPerson;   // Create another person without any identity
  ifstream infile(fname, ios::binary);   // Open the file for reading
  if(!infile)
  {
     cerr << "Couldn't open data file for reading" << endl;
     return 1;
  }
  // Read in the data previously stored in the file
  // Notice the cast of &bPerson to char *. I used the new C++ style
  // cast here, but you could use the old C way as shown above, too.
  infile.read((char*)&bPerson, sizeof(Person));
  infile.close(); // Close the stream
  // Display the data read to see if it is the same as written...
  cout << "bPerson.name: " << bPerson.name << '\n';
  cout << "bPerson.age:  " << bPerson.age << endl;
  return 0;
}
> ] OOPS! I guess they DO fall off the edge of the world........
---
---------------
* Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.