0

I want to read only the IP's and printf them without blank spaces.Here is part of my code :

 char buffer[256]
    dns_serv = fopen("dns_servers.conf", "rt"));
    log = fopen("logfile", "at")) ; 
    if (!fgets(buffer,sizeof(buffer), dns_serv)) {
                break;
            }       

            if (buffer[0] == '#') continue;
            if (buffer[0] == '\n') continue;

                if ((sockdns = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
                printf("Error\n");
                continue;
            }

            fprintf(log, "; %s - %s %s",buffer,argv[1], argv[2]);

This writes me on logfile :

; 141.85.128.1
 - google.com SOA

but i want to write it on the same line:

 ; 141.85.128.1 - google.com SOA

Where is my mistake? Here is the dns_servers.conf:

# first example
141.85.128.1

# another
# 61.34.189.250
john
  • 47
  • 1
  • 1
  • 3

4 Answers4

2

Here is solution for you using standard C++ library, so:

#include <fstream>
using namespace std;

Next instead of:

dns_serv = fopen("dns_servers.conf", "rt"));
log = fopen("logfile", "at")) ; 

write:

ifstream dns_serv("dns_servers.conf", std::ifstream::in);
ofstream log("logfile", std::ofstream::out | std::ofstream::app);

Instead of:

if (!fgets(buffer,sizeof(buffer), dns_serv)) {
            break;
        }

write:

dns_serv.getline (buffer, sizeof(buffer));
if (dns_serv.fail ()) break;

and finally instead of:

fprintf(log, "; %s - %s %s",buffer,argv[1], argv[2]);

write:

log << "; " << buffer << " - " << argv[1] << " " << argv[2]; // << endl;

Hope that it helped you :) I know I ruined your C style, but little C++ discipline is not bad. ;)

Ognjen Stanić
  • 505
  • 8
  • 17
1

fgets places the '\n' (end-of-line) character into its input buffer. You should remove it (but beware of cases when '\n' is not placed into the buffer, e.g. when a line is too long for a given buffer and fgets stops reading before it reaches end of line, or because fgets reaches end of file).

nullptr
  • 11,008
  • 1
  • 23
  • 18
0

It's because you also output the '\n' at the end of the line contained in buffer (retrieved by fgets)

You need to remove it from the string, doing something like that:

  int len;

  len = strlen(buffer);
  buffer[len - 1] = '\0';

Of course it might be wise to add more checks. And maybe use a more OO approach using strings class.

Maresh
  • 4,644
  • 25
  • 30
0

This code won't include '\n' at end of the string.

C++ Code:

#include <iostream>
#include <fstream>
using namespace std;

string line;
ifstream dns_serv ("dns_servers.conf");
ofstream log ("logfile");
if(dns_serv.is_open())
{
     while(dns_serv.good())
     {
          getline(dns_serv,line);
          if(line.at(0)=='#' || line.at(0)=='\n') continue;
          cout << line << endl;
          log << "; " << line << " - " << argv[1] << " " << argv[2] << endl;       
     }                       
}
dns_serv.close();
log.close();

in extra you can use some string trimming functions to remove extra white spaces. see this What's the best way to trim std::string?

Community
  • 1
  • 1
jad-panda
  • 2,509
  • 16
  • 22
  • Where does `ifstream`, `ofstream`, `getline`, etc. come from? The names aren't fully qualified and there's no `using` statement. It doesn't include the new line character but you don't say why. These things are important. – Captain Obvlious May 25 '13 at 12:01
  • @CaptainObvlious thanks for the advice. i will keep that in my mind. – jad-panda May 25 '13 at 12:19