![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 7
Rep Power: 0
![]() |
Problem writing to files in C++
Hi, im currently working on a program which requires me to write to a text file. when i use the fputs or fprintf command i get no error returned, but when i search the file the string that i entered with fputs isn't there, and i can't for the life of me figure out why. I'm new to C++, so if anyone has suggestions i'd appreciate it. THX.
P.S. I've included my code so far below: #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <fstream> FILE *stream; char str1[13]; char str2[13]={'"','S','t','a','r','t',' ','P','a','g','e','"','='}; char str3[100]; int count; int main() { //system("REGEDIT.EXE /E \"registry.REG\""); //rename("registry.REG","registry.txt"); stream=fopen("registry.txt","r+"); //Opens file. if (stream != NULL) //Ensures file has been opened successfully. { rewind(stream); //Sets the cursor position to the beginning of file. while (feof(stream)==0) //Loops until EOF has been reached. { fgets(str3,255,stream); //Gets next line of text from file. if (strlen(str3)>=13) //Ensures line at least 13 characters long. { for (count=0;count<=12;count++) /*Puts first 13 characters of line into str1.*/ { str1[count]=str3[count]; } printf("%s\n",str1); if (strcmp(str1, str2) == 0) //Checks if str1 equals str2. { fputs("\"Start Page\"=\"http://www.startrek.com/\"\n",stream); //printf("\"Start Page\"=\"http://www.startrek.com/\"\n"); //printf("%i\n",ftell(stream)); printf("Success!\n"); getchar(); } } } fclose(stream); //Closes File. } return EXIT_SUCCESS; } |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Stick some error-handling code in - for every if, there should be an else that prints out where it went wrong. For example, as a counterpart to the very first if statement, add this to the end:
else
{
printf("ERROR: Could not open the file.\n");
return EXIT_FAIL;
} |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 7
Rep Power: 0
![]() |
The program does get to the point where it prints "success!", and it is at the point in the file where the "Start Page"= is in the registry.txt file, so all the ifs up to that point must be true.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2005
Posts: 7
Rep Power: 0
![]() |
hI, I finally figured out what was wrong with my program here and got it working. For some reason if i opened a stream and read from it, i couldnt write to it later. So i opened two identical files, one i read from, the other i write too, problem solved. Out of curiousity, does anyone know why i couldn't
read and write to the same stream? Here's my updated code, notice that im using fstream now: #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <string> #include <iostream> #include <fstream> using namespace std; ifstream stream; ofstream stream2; char str1[13]; const char str2[14]={'"','S','t','a','r','t',' ','P','a','g','e','"','='}; char str3[100]; const char str4[40]={'"','S','t','a','r','t',' ','P','a','g','e','"','=','"','h','t','t','p',':','/','/','w','w','w','.','s','t','a','r','t','r','e','k','.','c','o','m','/','"'}; int cnt; int main(int argc, char *argv[]) { //system("REGEDIT.EXE /E \"C:\\windows\\registry.REG\""); //system("REGEDIT.EXE /E \"C:\\windows\\registry2.REG\""); stream.open("C:\\windows\\registry.reg"); stream2.open("C:\\windows\\registry2.reg"); if (stream != NULL) { while (!stream.eof()) { stream.getline(str3,1000); if (strlen(str3)>=13) { for (cnt=0;cnt<13;cnt++) { str1[cnt]=str3[cnt]; } if (strcmp(str1,str2)==0) { stream2 << str4 << endl; } } } stream.close(); stream2.close(); } return EXIT_SUCCESS; } |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
Hey, i organized your code and made some changes to it.
When you are using fstream libraries, the method to check to see if it's open or not isn't checking it against NULL. It's ([i/o]fstream).good(). And there is one other way, i think it might be the .bad() class member but i'm not sure. But besides that i made some changes on the way the program flowed, i think this is what you were trying to do. I may be wrong though. #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
char str1[13];
char str2[]="\"Start Page\"=";
char str3[100];
char str4 = "\"Start Page = http://www.startrek.com/\"";
int cnt;
int main(int argc, char *argv[]) {
ifstream stream;
ofstram stream2;
stream.open("C:\\windows\\registry.reg");
stream2.open("C:\\windows\\registry2.reg");
if (stream.good() && stream2.good()) {
while (!stream.eof()) {
stream.getline(str3,100);
if (strlen(str3)>=13)
for(cnt=0;cnt<strlen(str3);cnt++) {
for(int i=0;i<13;i++)
str1[i] = str3[cnt+i];
if(strcmp(str1, str) == 0) {
stream2 << str4 << endl;
break;
}
}
}
} else
return EXIT_FAILURE;
stream.close();
stream2.close();
return EXIT_SUCCESS;
} |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2005
Posts: 7
Rep Power: 0
![]() |
Solved. Thx for all your help people.
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() ![]() |
why make this so complicated?
....
#include <fstream>
....
....
//===============================================================
// WRITE TO FILE
//===============================================================
// Appends to existing file.
void WriteToFile (string strFile, string line)
{
fstream theFile;
theFile.open(strFile.c_str(),std::ios::app);
theFile << line << endl;
theFile.close();
}
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
If we're talking simplification...
void writeToFile(char *file, char *str) {
FILE *out = fopen(file, "w");
fwrite(out, str);
fclose(out);
} |
|
|
|
|
|
#9 |
|
Programming Guru
![]() ![]() ![]() |
Pointers scare people away man... I like my way better
![]()
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|