473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem in IP address conversion

im implementating a over lay network. This is the routing table given as a .txt file.
node no IP Port no
1 987.000.333.444 5000
2 987.111.333.444 7440
3 222.222.111.111 7000

This data is stored in t.txt and all data are separated by Tab space.
I wrote a program to read the file and store data separately. It reads node no and port no correctly but not the IP. And it reads last IP correctly

this is the programme
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <arpa/inet.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <netdb.h>        // gethostbyname()
  8.  
  9. int main() {
  10.   FILE *file;
  11.   FILE *file1;
  12.   char c[50]; /* make sure it is large enough to hold all the data! */
  13.   char *d;
  14.   char a,b,e,f;
  15.   int flag=0;
  16.   int m,n=0;
  17.   int i=0;
  18.   int j=0;
  19.  
  20. struct Node_Infor
  21. {
  22. unsigned short Node_No[10];
  23. struct in_addr IP[10];
  24. //unsigned long IP[10];
  25. unsigned short port[10];
  26. }Node;
  27.  
  28.  
  29.  
  30.   file = fopen("t.txt", "r");
  31.   if(file==NULL)
  32.   {
  33.     printf("Error: can't open file.\n");
  34.     return 1;
  35.   }
  36.   else
  37.   {
  38.     printf("File opened successfully.\n");
  39.  
  40.     while(feof(file)== 0)
  41.     {
  42.  
  43.                 c[i] = getc(file);
  44.  
  45.                 if(c[i] == '\t'||c[i]== '\n')
  46.                 {
  47.                         c[i] = '\0';
  48. //                      printf("%s\n",c);         /* print out the string      */
  49.                         i=0;
  50.                         flag++;
  51.  
  52.                         switch(flag)
  53.                         {
  54.                         case 1:
  55.                         Node.Node_No[n]=atoi(c);
  56.                         break;
  57.                         case 2:
  58.                         inet_aton(c,&Node.IP[n]);
  59.                         printf("input c: %s\nNode.IP: %s\n",c,inet_ntoa(Node.IP[n]));
  60.                         break;
  61.                         case 3:
  62.                         Node.port[n]=atoi(c);
  63.                         flag=0;
  64.                         n++;
  65.                         break;
  66.                         }
  67.  
  68.                 }
  69.                 else
  70.                 {
  71.                 i++;
  72.                 }
  73.     }
  74.     fclose(file1);
  75.     fclose(file);
  76.  
  77.         for(m=0;m<n;m++)
  78.         {
  79.                printf("%d\t%s\t%d\n",Node.Node_No[m],inet_ntoa(Node.IP[m]),Node.port[m]);
  80.         }
  81.     return 0;
  82.   }
  83. }
Feb 24 '08 #1
1 1476
gpraghuram
1,275 Expert 1GB
Read the IP address asa string and then store that to the in_addr structure.


Raghuram
Feb 25 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: John | last post by:
Hello all, I am trying to read in lines into a buffer from a file. Normally I would do this very low-level, but I have come to the conclusion I must stop doing everything the hard way. So, I...
12
by: johny smith | last post by:
I am trying to figure out a way to print the address of what called a certain function once inside the function. I am assuming that this information is on the stack somewhere. But can someone...
21
by: Stephen Biggs | last post by:
Given this code: void f(void){} int main(void){return (int)f+5;} Is there anything wrong with this in terms of the standards? Is this legal C code? One compiler I'm working with compiles this...
7
by: hierro | last post by:
I have a list of functions (all with suffix T). For each one, I need to implement the following: FunctionT() { if (some_condition) { // do some conversion first FunctionW(); // then do some...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
16
by: jimjim | last post by:
#include <stdio.h> #include <stdlib.h> //WARNING: The function's signature should be: f( int ** ) void f(int *ip) { static int dummy = 5; *ip = &dummy; } int main(){
8
by: SpreadTooThin | last post by:
Basically I think the problem is in converting from a 32 bit integer to a 16 bit integer. I have two arrays: import array a = array.array('L', ) b = array.array('H', ) b = a
15
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.