1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include "stdafx.h"
#include <ctype.h>
#include "stdio.h"
#include "stdlib.h"
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void ReadFile();
FILE *write;
struct Car {
string CarName;
float Acceleration;
int TopSpeed;
float TempMod;
float OilMod;
float RpmMod;
Car() {
// Default Constructor
CarName = "Default Car";
Acceleration = 200;
TopSpeed = 100;
TempMod = 500;
OilMod = 600;
RpmMod = 3500;
};
};
Car cars[6];
int _tmain(int argc, _TCHAR* argv[])
{
ReadFile();
return 0;
}
void ReadFile() {
FILE *read;
int c;
// open file to write/append to new/existing file
// this file is used for testing purposes
if((write = fopen ("testingcars.txt", "w")) == NULL) {
printf("Error Creating File.\n");
getchar();
exit(1);
}
fprintf(write, "testingcars.txt, Error and Feedback log, testing cars.txt:\n\n");
// opens file, if can't open, moan about it
if((read = fopen ("cars.txt", "r")) == NULL) {
//printf("Cannot open file.\n");
fprintf(write, "Error opening cars.txt");
getchar();
exit(1);
}
while( (c = fgetc(read)) != EOF) {
fprintf(write, "%c", c);
}
fprintf(write, "\n\nArray Data:\n\n");
string Name;
float Acc;
int Top;
float Temp;
float Oil;
float Rpm;
ifstream fin("cars.txt");
string line;
int CountCars = 0;
while (getline(fin, line) && CountCars <6) {
cout << line << endl;
istringstream tokenizer(line);
string token;
getline(tokenizer, token, ',');
istringstream s_iss_Name(token);
s_iss_Name >> Name;
cars[CountCars].CarName = Name;
cout << cars[CountCars].CarName << endl;
fprintf(write, "%s,", cars[CountCars].CarName);
getline(tokenizer, token, ',');
istringstream f_iss_Acc(token);
f_iss_Acc >> Acc;
cars[CountCars].Acceleration = Acc;
//cout << cars[CountCars].Acceleration << endl;
fprintf(write, "%f,", cars[CountCars].Acceleration);
getline(tokenizer, token, ',');
istringstream int_iss(token);
int_iss >> Top;
cars[CountCars].TopSpeed = Top;
//cout << cars[CountCars].TopSpeed << endl;
fprintf(write, "%d,", cars[CountCars].TopSpeed);
getline(tokenizer, token, ',');
istringstream f_iss_Temp(token);
f_iss_Temp >> Temp;
cars[CountCars].TempMod = Temp;
//cout << cars[CountCars].TempMod << endl;
fprintf(write, "%f,", cars[CountCars].TempMod);
getline(tokenizer, token, ',');
istringstream f_iss_oil(token);
f_iss_oil >> Oil;
cars[CountCars].OilMod = Oil;
//cout << cars[CountCars].OilMod << endl;
fprintf(write, "%f,", cars[CountCars].OilMod);
getline(tokenizer, token, ',');
istringstream f_iss_rpm(token);
f_iss_rpm >> Rpm;
cars[CountCars].RpmMod = Rpm;
//cout << cars[CountCars].RpmMod << endl;
fprintf(write, "%f\n", cars[CountCars].RpmMod);
CountCars++;
}
fclose(write);
fclose(read);
}
|