?
Solved

file reading ...line bye line

Posted on 2005-05-06
9
Medium Priority
?
241 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:talktome_prashanth
7 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13947123
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13947129
Sorry, detected a typo, please correct:
  char *replay_victim_ip, *replay_victim_mac, *replay_attacker_ip;

0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13947159

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13947223
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 13947241
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
 
LVL 4

Expert Comment

by:furqanchandio
ID: 13950041
hi

fgets is a good idea
0
 
LVL 4

Expert Comment

by:smpoojary
ID: 13957732
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question