• C

file reading ...line bye line

Can anyone help me in  a read operation wich reads the each line of below input file.
I dont want EOL line character to be included

input file:

snort.ftp.log
192.168.5.7
00:b0:d0:49:ce:5e
21
192.168.2.9
00:60:97:bf:d7:1f
1028
192.168.5.7
00:b0:d0:49:ce:5e
192.168.2.6
00:50:da:1c:0d:9d
eth0
delay

these must be read line by line  and stored in char arrays.
like  replay_victim_ip, replay_victim_mac.

declarations are done like this:
  replay_victim_ip = (char*) malloc(100*sizeof(char));
  replay_victim_mac = (char*) malloc(100*sizeof(char));
  replay_attacker_ip = (char*) malloc(100*sizeof(char));


thanks
talktome_prashanthAsked:
Who is Participating?
 
Jaime OlivaresSoftware ArchitectCommented:
Joining all:

#include <stdio.h>

char *ReadString(char *, int,  FILE *);

main()
{
  FILE *fp;
  char *replay_victim_ip, *replay_victim_mac, *replay_attacker_ip;

  fp = fopen("snort.ftp.log","r");

  while ( !feof(fp) ) {
       replay_victim_ip = (char*) malloc(100*sizeof(char));
       replay_victim_mac = (char*) malloc(100*sizeof(char));
       replay_attacker_ip = (char*) malloc(100*sizeof(char));

     ReadString(replay_victim_ip ,100, fp);
     ReadString(replay_victim_mac ,100, fp);
     ReadString(replay_attacker_ip,100, fp);

     /* DO SOMETHING WITH YOUR DATA HERE */
  }

  fclose(fp);
}

char *ReadString(char *buffer, int n,  FILE *stream)
{
      int len;
      if (fgets(buffer,n, stream)) {
           len = strlen(buffer);
           if (len && buffer[len-1]=='\n')
                buffer[len-1] = '\0';
           return buffer;
      } else
           return NULL;
}


0
 
Jaime OlivaresSoftware ArchitectCommented:
Just have to use fgets() function, something like this:

#include <stdio.h>

main()
{
  FILE *fp;
  char *replay_victim_ip, replay_victim_mac, replay_attacker_ip;

  fp = fopen("snort.ftp.log","r");

  while ( !feof(fp) ) {
       replay_victim_ip = (char*) malloc(100*sizeof(char));
       replay_victim_mac = (char*) malloc(100*sizeof(char));
       replay_attacker_ip = (char*) malloc(100*sizeof(char));

     fgets(replay_victim_ip ,100, fp);
     fgets(replay_victim_mac ,100, fp);
     fgets(replay_attacker_ip,100, fp);

     /* DO SOMETHING WITH YOUR DATA HERE */
  }

  fclose(fp);
}
0
 
Jaime OlivaresSoftware ArchitectCommented:
Sorry, detected a typo, please correct:
  char *replay_victim_ip, *replay_victim_mac, *replay_attacker_ip;

0
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
Kent OlsenData Warehouse Architect / DBACommented:

After reading each line, you'll also want to drop the eol character:

     fgets(replay_victim_ip ,100, fp);
     fgets(replay_victim_mac ,100, fp);
     fgets(replay_attacker_ip,100, fp);

     replay_victim_ip(strlen(replay_victim_ip) - 1) = 0;
     replay_victim_mac (strlen (replay_victim_mac) - 1) = 0;
     replay_attacker_ip (strlen (replay_attacker_ip) - 1) = 0;


Kent
0
 
Jaime OlivaresSoftware ArchitectCommented:
This was a first approach, but you have to consider that fgets() reads also the newline character, so you can make your own function:

char *ReadString(char *buffer, int n,  FILE *stream)
{
      int len;
      if (fgets(buffer,n, stream)) {
           len = strlen(buffer);
           if (len && buffer[len-1]=='\n')
                buffer[len-1] = '\0';
           return buffer;
      } else
           return NULL;
}
0
 
furqanchandioCommented:
hi

fgets is a good idea
0
 
smpoojaryCommented:
How can you distinguish Victim IP and Attacker IP from input file? Your file contains IP , MAC, eth0, delay, port number and etc. For this you have check the line and store it into corresponding data item.
-Mahesh

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.