Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-24-2004, 08:41 PM   #1 (permalink)
Caleb.Carlton
Registered User
 
Join Date: Oct 2004
Posts: 7
Caleb.Carlton is on a distinguished road
Text program

I need to make a program that opens a file full of proxies, for example

123.123.123.255:1080
123.123.123.123:1080
123.123.123.255:1080
123.123.123.123:1080

And take off the :1080

Any help would be much appreciated. Thank you.
Caleb.Carlton is offline   Reply With Quote
Old 10-25-2004, 12:07 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
redhead is on a distinguished road
Code:
#!/bin/sh
cat $1 | sed 's/:1080//g'
if you want some other way, be more specific as to if it's C or C++, since file handling is a bit different.
Perhaps you should look at opening file, reading in, string handling, and output. Then try to make something happen, if all fails, give us your suggestion and we will correct it, maybe even come with the solution.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-25-2004, 05:09 AM   #3 (permalink)
Caleb.Carlton
Registered User
 
Join Date: Oct 2004
Posts: 7
Caleb.Carlton is on a distinguished road
Sorry about the confusion, I was looking for information on how to do it in C++. Thank you for your help redhead.
Caleb.Carlton is offline   Reply With Quote
Old 10-25-2004, 06:07 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
redhead is on a distinguished road
something like this?
Code:
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::endl;

const int line_length = 200;

int main(int argv, char* argc[])
{
  char buffer[line_length];
  if(argv != 2)
    {
      cout << "Usage: " << argc[0] << " <filename>" << endl;
      return -1;
    }

  ifstream file (argc[1], ios::in);
  if (!file.is_open())
    {
      cout << "Error: opening file " << argc[1] << endl;
      return -1;
    }
  while (!file.eof() )
    {
      file.getline(buffer, line_length -1);
      strtok(buffer, ":");
      cout << buffer << endl;
    }
  file.close();
  return 0;
}
The use of strtok() should be thought through, since what I use it for here is using it's bad sideeffect for a good thing(tm).
strtok() changes it's first argument uppon first call, this will result in whatever is placed befor the : will remain in buffer, for a better usage, you'd probaly change it to:
Code:
int main(int argv, char* argc[])
{
  char buffer[line_length];
  char *ptr;
  if(argv != 2)
...
  while (!file.eof() )
    {
      file.getline(buffer, line_length -1);
      ptr = strtok(buffer, ":");
      cout << ptr << endl;
...
In this way you can make a check to see if it's a valid token found by strtok().

If you want it to show the port aswell, but seperated from the IP number, this might be useful:
Code:
...
  while (!file.eof() )
    {
      file.getline(buffer, line_length -1);
      ptr = strtok(buffer, ":");
      while(ptr != NULL)
        {
          cout << ptr << endl;
          ptr = strtok(NULL, "\t :");
        }
    }
...
This will also make a <tab> or <space> delimited file readable.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 10-25-2004 at 06:36 AM.
redhead is offline   Reply With Quote
Old 10-25-2004, 05:00 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I am just dropping by like the wind, in the mood for some relaxing code. I'll attend to unsolved posts later.

This part I wouldn't reccomend:
Code:
while (!file.eof() )
Actually it *might* be wrong to do such a thing. I'll explain.
The EOF-bit is set when the file reaches PAST the end-of-file! So the while loop checks one time too often for the true end of file. That is not persé wrong. The program still works. In fact, with simple file i/o operations there is nothing wrong with this.

But in general...
The eof-bit might not always be set! Therefore, this kind of testing in a loop is not ok.
If the input is mapped to a device, then it is not possible for C++ to predict what the last input was.

I'll get to the solution in a moment. First another thing.
Code:
ifstream file (argc[1], ios::in);
  if (!file.is_open())
In the first line, ios::in is not needed right now, since it is the default parameter for istream anyway. Just a heads up.
In the second line, testing explicitly with is_open() is not needed right now too. That means style-wise it is not needed. Since the handle to istream already knows the name of the file, we can use the overloaded operator!() for the ifstream class.

See code below for "solution" to these issues. The app by itself deals only with files as presented in the very first post by Caleb.Carlton, wich assumes correct "IP:port" format. Tabs, spaces etc will be dealt with correctly as well. An IP like "000" isn't usually defined by many programs, but both 0.0.0.0:port as well as 000.000.000.00:port will work.
You could implement features to check for correct ip:port formats but that wasn't asked by the poster, so I didn't.
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>


using std::cout;
using std::endl;
using std::ifstream;
using std::string;
using std::getline;
using std::size_t;

int main(int argv, char* argc[])
{
   if(argv != 2)
   {
      cout << "Usage: " << argc[0] << " <filename>" << endl;
      return -1;
   }

   ifstream file(argc[1]);
   if (!file)
   {
     cout << "Error: opening file " << argc[1] << endl;
     return -1;
   }
   
   string ipNumber;
   while ( file>>ipNumber )
   {
      size_t pos;
      pos = ipNumber.rfind(":");
      ipNumber.erase(pos, ipNumber.size()-1);
      cout << ipNumber << endl;
   }
   
   //If file-reading loop exits before an EOF has been reached, something is wrong.
   if(!file.eof())
   {
      cout<<"ERROR: Something went wrong during processing of file."<<endl;
   }
   
   file.close();
   return 0;
}
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 06:14 AM
[MySQL] blob vs text redhead PHP 2 03-29-2004 11:15 PM


All times are GMT -8. The time now is 07:14 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting