Hello Steven.
28 May 97 16:15, Steven Dyck wrote to All:
#include
// We're using this for "cout"
#include
main()
{
// You should have a small '*' in front of Monster_name
// I'm surprised that you can make this program work, does i really
// print "Bear" on the screen? Normally it will only print the first
// character if you ommit the pointer '*'.
unsigned *char Monster_Name = "bear";
//Monster_Name = "Bear";
clrscr();
//printf("%s", Monster_Name);
// Well, if you're doing it in C++ why not:
cout << Monster_Name;
getch();
return(0);
}
SD> Any ideas how to get rid of this? I know it doesn't hurt the
SD> program,
SD> but when you have 20 or so of them, it gets anoying.
sure does...
SD> My next question is how do you use dos commands in C++. I mean if I
SD> wanted to make a program that could show all the files in a certain
SD> directory, or make directories, etc. how would I do this?
// ShowFiles.cpp
void main()
{
system("md c:\\mydir");
system("dir c:\\mydir");
}
But this is not the way to go, you should consider
using some of the build in functions instead of calling the
DOS commands:
findfirst, findnext and mkdir
note that using these functions will make
your program non-portable
SD> My last question is this. How do you save to a seperate file (name
SD> of
SD> person, age, other information), and how do you retrieve this
SD> information in the same program?
You can use the normal C functions, but you should consider using the
file stream objects from C++. I'm not familiar with these functions, and
will give you an example using C syntax.
// In this example we are using a textfile, the functions for
// working with binary files are a bit deifferent, but textfiles
// should be more portable than binary (dont know why).
////////////////////////////////////////////////////////
// Filetest.cpp
// File writing and reading example
// tested and compiled with MS visual C++ 4.0
// 32 bit console mode.
#include
#include
void main()
{
FILE *f;
// Open with "w+", that means Writing
if( (f = fopen( "file.txt", "w+" )) == NULL ) // Do some errorchecking
{
printf("The file 'file.txt' was not opened\n");
return;
}
// It is not nessacary to use "const", but it will not hurt
const char *name = "Rupert the Cat";
const int age = 12;
// Write the data
fprintf(f, "Name = %s and Age=%d", name, age);
fclose(f); // Close the file
// To read from the file we will use a different method
// just for the heck of it :-)
// Open with "r+", that means reading
if( (f = fopen( "file.txt", "r+" )) == NULL ) // Do
{
printf("The file 'file.txt' was not opened\n");
return;
}
// How big is the file?
fpos_t filesize;
fseek(f, 0, SEEK_END); // go to end of file
fgetpos(f, &filesize); // read the position
fseek(f, 0, SEEK_SET); // go to start of file
// Make some memory to hold the contents of the file
// in a text stream
char *str = new char[filesize + 1];
int c; // this will hold the characteres from the stream
for (int i=0 ; i Thank-you very much to those who answer my questions. I know they
SD> might seem trivial, but suggestions would be greatly appreciated!
SD> Thanks. Steve
We all have to start somewhere, and in my opinion
C_PLUSPLUS is a good place.
Christian
--- GoldED 2.50 UNREG
---------------
* Origin: Men i aften skal det v‘re anderledes... (2:235/335.22)
|