5 Replies - 5154 Views - Last Post: 30 October 2009 - 04:55 AM Rate Topic: -----

#1 mebeatyou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-October 09

Reading/Altering remote file

Posted 29 October 2009 - 12:34 PM

Hello, I'm new to the community, and was wondering if I might get some help with a small project I'm working on.
I'm fairly new to C++, however I'm familiar with programming concepts and how things work.
It seems that I've hit a rut in my coding progress however.

The idea of the code I have put together, is to open an HTML file (source.html), and extract all red numbers and save them to a text file (red.txt).
This part works flawlessly, it even converts fractions in word form (one-half, etc.).
I know it is sloppy code, and it is sure to be optimized later, but for now I have a new problem.

So, here is the theory:
In a webpage I have upload access to, I have this file: " http://home.fuse.net/reaper/test.txt "
Here is a list of "passwords", or words that will work for my program.
At the beginning of the program will be something like:

cout << "Input password";
cin >> password;

Then if the password given matches any of the words in the file on my website, the program runs, if not, it shuts down.
NOW, after given a proper password, I was hoping there would be someway to rewrite the "test.txt" to remove the word used, so that each password may be used once and only once.
I have a feeling that this is all possible, just hoping it's not too out of my league.
I ran a search through the website for something to help me, and couldn't find anything to quite solve the problem I don't think, so I apologize if this is out of place. Anyways, here is what I have so far.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	int a=0,b=0, test=0;
	const int LENGTH = 150000;
	ifstream inFile;
	ofstream outFile("red.txt");
	char source[LENGTH], ch, ch2, ch3;

	inFile.open("Source.html", ios::in);
	if (!inFile)
	{
		cout << "Cannot open file";
		return 0;
	}
	else cout << "File Opened" << endl;


	while (!inFile.eof() && a < LENGTH)
	{
		a=a+1;
		inFile.get(ch);
		cout << ch;
		source[a] = ch;

	}

	a=0;

	while(a < LENGTH)
	{
		if ((source[a]=='<')
		 && (source[a+1]=='f')
		 && (source[a+2]=='o')
		 && (source[a+3]=='n')
		 && (source[a+4]=='t')
		 && (source[a+5]==' ')
		 && (source[a+6]=='c')
		 && (source[a+7]=='o')
		 && (source[a+8]=='l')
		 && (source[a+9]=='o')
		 && (source[a+10]=='r')
		 && (source[a+11]=='=')
		 && (source[a+13]=='r')
		 && (source[a+14]=='e')
		 && (source[a+15]=='d')
		 && (source[a+17]=='>'))
		 {
			test=1;
		 }
			if (test==1)
			{
				cout << "	 Found one!" << endl;
				b=a+18;
				while (source[b]!='<')
				{

					ch2=source[b];
					if ((source[b]=='o')
					&& (source[b+1]=='n')
					&& (source[b+2]=='e')
					&& (source[b+4]=='h')
					&& (source[b+5]=='a')
					&& (source[b+6]=='l')
					&& (source[b+7]=='f'))
					{
						ch2='0';
						outFile.put(ch2);
						ch2='.';
						outFile.put(ch2);
						ch2='5';
						outFile.put(ch2);
						b=b+8;
					}
					else if ((source[b]=='o')
					&& (source[b+1]=='n')
					&& (source[b+2]=='e')
					&& (source[b+4]=='t')
					&& (source[b+5]=='h')
					&& (source[b+6]=='i')
					&& (source[b+7]=='r')
					&& (source[b+8]=='d'))
					{
						ch2='0';
						outFile.put(ch2);
						ch2='.';
						outFile.put(ch2);
						ch2='3';
						outFile.put(ch2);
						ch2='3';
						outFile.put(ch2);
						ch2='3';
						outFile.put(ch2);
						ch2='3';
						outFile.put(ch2);
						ch2='3';
						outFile.put(ch2);
						ch2='3';
						outFile.put(ch2);
						ch2='3';
						outFile.put(ch2);
						ch2='3';
						outFile.put(ch2);
						b=b+9;
					}
					else if ((source[b]=='o')
					&& (source[b+1]=='n')
					&& (source[b+2]=='e')
					&& (source[b+4]=='f')
					&& (source[b+5]=='o')
					&& (source[b+6]=='u')
					&& (source[b+7]=='r')
					&& (source[b+8]=='t')
					&& (source[b+9]=='h'))
					{
						ch2='0';
						outFile.put(ch2);
						ch2='.';
						outFile.put(ch2);
						ch2='2';
						outFile.put(ch2);
						ch2='5';
						outFile.put(ch2);
						b=b+10;
					}
					else if ((source[b]=='t')
					&& (source[b+1]=='w')
					&& (source[b+2]=='o')
					&& (source[b+4]=='t')
					&& (source[b+5]=='h')
					&& (source[b+6]=='i')
					&& (source[b+7]=='r')
					&& (source[b+8]=='d')
					&& (source[b+9]=='s'))
					{
						ch2='0';
						outFile.put(ch2);
						ch2='.';
						outFile.put(ch2);
						ch2='6';
						outFile.put(ch2);
						ch2='6';
						outFile.put(ch2);
						ch2='6';
						outFile.put(ch2);
						ch2='6';
						outFile.put(ch2);
						ch2='6';
						outFile.put(ch2);
						ch2='6';
						outFile.put(ch2);
						ch2='6';
						outFile.put(ch2);
						ch2='6';
						outFile.put(ch2);
						b=b+10;
					}
					else if ((source[b]=='t')
					&& (source[b+1]=='h')
					&& (source[b+2]=='r')
					&& (source[b+3]=='e')
					&& (source[b+4]=='e')
					&& (source[b+6]=='f')
					&& (source[b+7]=='o')
					&& (source[b+8]=='u')
					&& (source[b+9]=='r')
					&& (source[b+10]=='t')
					&& (source[b+11]=='h')
					&& (source[b+12]=='s'))
					{
						ch2='0';
						outFile.put(ch2);
						ch2='.';
						outFile.put(ch2);
						ch2='7';
						outFile.put(ch2);
						ch2='5';
						outFile.put(ch2);
						b=b+10;
					}
					else
					{
						outFile.put(ch2);
						cout << ch2;
						b=b+1;
					}
				}
				outFile << endl;
				test=0;
			}
		a=a+1;
	}


	inFile.close();
	outFile.close();




	cout << "Reading and writing of files completed.\n";
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Reading/Altering remote file

#2 EdwinNameless   User is offline

  • D.I.C Addict
  • member icon

Reputation: 128
  • View blog
  • Posts: 723
  • Joined: 15-October 09

Re: Reading/Altering remote file

Posted 29 October 2009 - 12:43 PM

This is all very confusing. Do you need to read the file from your website, and then upload it again, or is it all local? What is the link with the "red numbers"? Have you looked into regular expressions rather than parsing the file character by character?
Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Reading/Altering remote file

Posted 29 October 2009 - 01:33 PM

Can ou just save the html file on the drive and use it or do you want to open up a socket to read the file off of the server? its quite simple if you already have the file on the drive.
Was This Post Helpful? 0
  • +
  • -

#4 mebeatyou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-October 09

Re: Reading/Altering remote file

Posted 29 October 2009 - 06:42 PM

View PostImaSexy, on 29 Oct, 2009 - 12:33 PM, said:

Can ou just save the html file on the drive and use it or do you want to open up a socket to read the file off of the server? its quite simple if you already have the file on the drive.


The html file "Source.html" is already on my drive.
The program right now is working entirely properly, and the way I want.
Source.html is already in the directory of the program, and red.txt is created upon the program's completion.

It is reading the file off my server, which apparently I have to open a socket to do.
I want to be able to READ FROM and WRITE TO a file that is located on a webpage that I have access to.
Was This Post Helpful? 0
  • +
  • -

#5 EdwinNameless   User is offline

  • D.I.C Addict
  • member icon

Reputation: 128
  • View blog
  • Posts: 723
  • Joined: 15-October 09

Re: Reading/Altering remote file

Posted 30 October 2009 - 01:26 AM

View Postmebeatyou, on 29 Oct, 2009 - 05:42 PM, said:

I want to be able to READ FROM and WRITE TO a file that is located on a webpage that I have access to.


Ironically, I have posted a snippet that uses libcurl to read off a website (just as you want to do), but it's not been approved (yet?). libcurl can also be used to put a file on FTP (see this link for an example): if I get motivated, I might post another snippet for this.
Was This Post Helpful? 0
  • +
  • -

#6 mebeatyou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-October 09

Re: Reading/Altering remote file

Posted 30 October 2009 - 04:55 AM

View PostEdwinNameless, on 30 Oct, 2009 - 12:26 AM, said:

View Postmebeatyou, on 29 Oct, 2009 - 05:42 PM, said:

I want to be able to READ FROM and WRITE TO a file that is located on a webpage that I have access to.


Ironically, I have posted a snippet that uses libcurl to read off a website (just as you want to do), but it's not been approved (yet?). libcurl can also be used to put a file on FTP (see this link for an example): if I get motivated, I might post another snippet for this.


That link you posted looks almost exactly like something I'm looking for...
It can read from a file, as well as rewrite it while on my website (I think?)

I'd love some help understanding how it works, and how to change it for my needs.. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1