English 中文(简体)
将案文文件翻译成结构
原标题:reading a text file into a structure

我的文本档案也一样(档案中的数据也可能增加)

    822
    172.28.6.56
    172.34.34.53
    3244
    5434

    844
    192.150.160.145
    192.67.45.123
    2344
    234


    700
    192.13.56.6
    192.89.95.3
    4356
    3566

    522
    172.28.6.137
    172.28.6.110
    2543
    5245

    900
    255.255.255.255
    244.244.244.244
    2435
    3245

我的结构也好像这样。

    struct input_par
    {
    int key_node;
    char src_ip[15];
    char dst_ip[15];
    int src_port;
    int dst_port;
    };

i have to fill this struture from data stored in file, once it completes inputting the 5 memebers of the structure i have to send this structure to a function i.e, i fill the structure with

    822
    172.28.6.56
    172.34.34.53
    3244
    5434

然后,在将这一结构交给职能后,即再次将结构输入档案中下一个数据,即:

    844
    192.150.160.145
    192.67.45.123
    2344
    234

I have to keep doing this till i reach EOF. I used fscanf it is not working properly how to do this? pls help

问题回答

只要这些结构之间实际上只有一个空间...... 我可能这样做。 很粗略地说,只要你知道你的最高线大小,而且档案结构确实没有任何干扰(外线空白等)。 否则,如果是例外情况,你就更不用写更好的法典,处理和打破/恢复。

As the comments note, you ll want to make sure to increase your char array s to account for the at the end. Here s some modifications to what I originally posted. Note, this may break if the line endings are Windows based (I believe). I added a little hack to try to correct the issue of array space and characters.

struct input_par
{
int key_node;
char src_ip[21];
char dst_ip[21];
int src_port;
int dst_port;
};


FILE* file = fopen("inputfile.txt", "r");
char linebuffer[21]; //a little extra room here than needed, presumably...
struct input_par ip;
int i = 0, l = 0;

while(fgets(linebuffer, 20, file))
{
   ip.key_node = atoi(linebuffer);
   fgets(linebuffer, 20, file);
   strcpy(ip.src_ip, linebuffer);

   l = strlen(linebuffer);
   for(i = 0; i < l; ++i){
      if(linebuffer[i] ==  
 ){
         linebuffer[i] =   
         break;
      }
   }

   fgets(linebuffer, 20, file);
   strcpy(ip.dest_ip, linebuffer);

   l = strlen(linebuffer);
   for(i = 0; i < l; ++i){
      if(linebuffer[i] ==  
 ){
         linebuffer[i] =   
         break;
      }
   }

   fgets(linebuffer, 20, file);
   ip.src_port = atoi(linebuffer);
   fgets(linebuffer, 20, file);
   ip.dst_port = atoi(linebuffer);
   fgets(linebuffer, 20, file); //get the empty line
}

你们可以用一个电话读到整个结构,以进行扫描。

---和

while(!feof(fp))
{
    input_par ip;
    int ret = fscanf(fp,"%*[ 	
]%d%*[ 	
]%s%*[ 	
]%s%*[ 	
]%d%*[ 	
]%d",&ip.key_node,&ip.src_ip,&ip.dst_ip,&ip.src_port,&ip.dst_port);
    if (ret !=5)
    {
        //skip this record
        continue;
    }
    //process record ip
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签