Well, my program compiles right, but crashes when I try to run it.
*Long Code Ahead*
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int writeini (char filename[20], char division[20], char section[30], char value[90]) {
char aline[125];
char sline[125];
int before_val;
ofstream ini_write ((strcat("temp",filename)), ios::out);
//validity checker--checks to see if it should replace or add the new information
ifstream ini_check (filename, ios::in);
if ( !ini_check.is_open() ) {
return 1; //returns error "1" if file could NOT be opened
}
else {
int ch;
char div_check[30];
for (int x = 0, t = 0; (ch = cin.get()) != EOF; x++) {
cin.getline(aline,'\n');
if (t == 0) { //check if division and section exist; else set them to be created
if (aline[0] == '[') {
int lastchar = 0;
strcpy(div_check,"");
for (int y = 1; aline[y] != ']'; y++) {
if (aline[y] == ']') lastchar = --y; //assign 'lastchar' to the last character in the division name
}
if (lastchar > 1) {
int y = 1;
while (y <= lastchar) {
//div_check += aline[y]; //assembles the found division
y++;
}
}
if (strcmp(div_check,division) == 0) { //compares the found division with input division, if same moves on to section
t = 1;
}
strcpy(sline,aline);
}
}
else if (t == 1) { //checks division for matching section
strcpy(div_check,"");
if (aline[0] == '[') {
for (int z = 0; aline[z] != '='; z++) { //assembles all characters before '=' into div_check
div_check[z] = aline[z];
}
}
if (strcmp(div_check,section) == 0) { //if the user input 'section' = the assembled div_check, it readies sline to write to file.
strcpy(sline,section);
strcat(sline,"=");
strcat(sline,value);
t = 4;
}
else if (aline[0] == '[') { //if the division is ending and a new starting, it writes sline before going on to copy the rest of the file.
strcpy(sline,section);
strcat(sline,"=");
strcat(sline,value);
ini_write << sline << "\n";
strcpy(sline,aline);
t = 4;
}
}
else if (t == 4) { //writes the remaining portion of the file after edit
strcpy(sline,aline);
}
ini_write << sline << "\n";
strcpy(sline,"");
}
}
ini_write.close();
ini_check.close();
}
int main (int argc, char *argv[])
{
char hi[30], hey[30], ho[30], hehe[30];
cout << "Hello there. 1st: ";
cin >> hi;
cout << "2nd: ";
cin >> hey;
cout << "3rd: ";
cin >> ho;
cout << "4th: ";
cin >> hehe;
writeini(hi,hey,ho,hehe);
cout << "writeini(" << hi << "," << hey << "," << ho << "," << hehe << ");";
system("PAUSE");
return EXIT_SUCCESS;
}
EDIT: Oh, the program goes through and asks for 1st, 2nd, 3rd, and 4th, then crashes. (Apparently while running the function)