| Concept |
C Example |
C++ Example |
| IO Library |
#include <stdio.h>
|
#include <iostream.h>
|
| Input |
int i;
float f;
char s[20];
scanf("%d %f %s", &i, &f, s);
|
int i;
float f;
char s[20];
cin << i << f << s;
|
| Output |
int i = 1;
float f = 2.5;
char s[] = "A string";
printf("Vars: %d, %f, %s", i, f, s);
|
int i = 1;
float f = 2.5;
char s[] = "A string";
cout << "Vars: " << i << ", " << f << ", " << s;
|
| File IO Library |
#include <stdlib.h>
|
#include <fstream.h>
|
| File Input |
//Pointer to file
FILE *fp;
//data for reading
int i;
float f;
char s[20];
//Open file for reading
fp = fopen("myfile.txt", "r");
//Check if file could be opened
if (fp == NULL)
{
printf("Could not open file.\n");
return -1;
}
//read in data
fscanf("%d %f %s",&i, &f, s);
//Close the file
fclose(fp)
|
//Pointer to file
ifstream iFile;
//data for reading
int i;
float f;
char s[20];
//Open file for reading
iFile.open("myfile.txt", ios::in);
//Check if file could be opened
if (!iFile)
{
cerr << "Could not open file.\n";
exit (-1);
}
//Read in data
iFile >> i >> f >> s;
//Close the file
iFile.close()
|
| File Output |
//Pointer to file
FILE *fp;
//data for writing
int i = 1;
float f = 0.5;
char s[] = "A String";
//Open file for writing
fp = fopen("myfile.txt", "w");
//Check if file could be opened
if (fp == NULL)
{
printf("Could not open file.\n");
return -1;
}
//read in data
fprintf(fp,"%d %f %s",&i, &f, s);
//Close the file
fclose(fp)
|
//Pointer to file
ofstream oFile;
//data for reading
int i = 1;
float f = 0.5;
char s[] = "A String";
//Open file for writing
oFile.open("myfile.txt", ios::out);
//Check if file could be opened
if (!oFile)
{
cerr << "Could not open file.\n";
exit (-1);
}
//Read in data
oFile << i << f << s;
//Close the file
oFile.close()
|
| Memory Allocation |
struct studentRecord {
char fName[];
char lName[];
int ID;
};
struct studentRecord *s;
s = (struct studentRecord *)malloc(sizeof(struct studentRecord));
|
struct studentRecord {
char fName[];
char lName[];
int ID;
};
struct studentRecord *s;
s = new studentRecord;
|
| Memory Deallocation |
struct studentRecord {
char fName[];
char lName[];
int ID;
};
struct studentRecord *s;
s = (struct studentRecord *)malloc(sizeof(struct studentRecord));
//Some code
free(s);
|
struct studentRecord {
char fName[];
char lName[];
int ID;
};
struct studentRecord *s;
s = new studentRecord;
//Some code
delete s;
|
| Memory Allocation (Multiple elements) |
struct studentRecord {
char fName[];
char lName[];
int ID;
};
int i;
//s is an array of pointers to studentRecords.
struct studentRecord *s[];
for(i = 0; i < NUM_ELEMENTS; i++)
s[i] = (struct studentRecord *)malloc(sizeof(struct studentRecord));
|
struct studentRecord {
char fName[];
char lName[];
int ID;
};
struct studentRecord *s[];
for(int i = 0; i < NUM_ELEMENTS; i++)
s[i] = new studentRecord;
|
| Memory Deallocation (Multiple elements) |
struct studentRecord {
char fName[];
char lName[];
int ID;
};
int i;
//s is an array of pointers to studentRecords.
struct studentRecord *s[];
for(i = 0; i < NUM_ELEMENTS; i++)
s[i] = (struct studentRecord *)malloc(sizeof(struct studentRecord));
//Some code
for(i = 0; i < NUM_ELEMENTS; i++)
free(s[i]);
|
struct studentRecord {
char fName[];
char lName[];
int ID;
};
struct studentRecord *s[];
for(int i = 0; i < NUM_ELEMENTS; i++)
s[i] = new studentRecord;
//Some code
delete[] s;
|