Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 27th, 2005, 12:13 AM   #1
Klick
Newbie
 
Join Date: Mar 2005
Posts: 7
Rep Power: 0 Klick is on a distinguished road
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;
}
Klick is offline   Reply With Quote
Old Mar 28th, 2005, 7:43 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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;
	}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 28th, 2005, 11:55 AM   #3
Klick
Newbie
 
Join Date: Mar 2005
Posts: 7
Rep Power: 0 Klick is on a distinguished road
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.
Klick is offline   Reply With Quote
Old Mar 30th, 2005, 1:08 AM   #4
Klick
Newbie
 
Join Date: Mar 2005
Posts: 7
Rep Power: 0 Klick is on a distinguished road
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;
}
Klick is offline   Reply With Quote
Old Mar 30th, 2005, 3:11 AM   #5
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
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;
}
tempest is offline   Reply With Quote
Old Mar 30th, 2005, 6:07 PM   #6
Klick
Newbie
 
Join Date: Mar 2005
Posts: 7
Rep Power: 0 Klick is on a distinguished road
Solved. Thx for all your help people.
Klick is offline   Reply With Quote
Old Mar 30th, 2005, 10:42 PM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Mar 30th, 2005, 11:07 PM   #8
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
If we're talking simplification...

void writeToFile(char *file, char *str) {
    FILE *out = fopen(file, "w");
    fwrite(out, str);
    fclose(out);
}
tempest is offline   Reply With Quote
Old Mar 31st, 2005, 11:59 AM   #9
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC