马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Jc嘻嘻 于 2017-9-16 16:20 编辑
大家好,
我想打开我这个txt文件。
1503251369.691375 84:1b:5e:a8:bf:7f 68:94:23:4b:e8:35 100
1503251374.195670 00:8e:f2:c0:13:cc 00:11:d9:20:aa:4e 397
1503251374.205047 00:8e:f2:c0:13:cc 00:11:d9:20:aa:4e 397
1503251374.551604 00:8e:f2:c0:13:cc 00:11:d9:20:aa:4e 157
1503251375.551618 84:1b:5e:a8:bf:7c cc:3a:61:df:4b:61 37
1503251375.552697 84:1b:5e:a8:bf:7c cc:3a:61:df:4b:61 37
1503251375.553957 84:1b:5e:a8:bf:7c cc:3a:61:df:4b:61 37
1503251375.555332 84:1b:5e:a8:bf:7c cc:3a:61:df:4b:61 37
1503251375.556352 84:1b:5e:a8:bf:7c cc:3a:61:df:4b:61 37
1503251375.557708 84:1b:5e:a8:bf:7c cc:3a:61:df:4b:61 37
1503251375.558756 84:1b:5e:a8:bf:7c cc:3a:61:df:4b:61 37
1503251376.426893 74:e2:f5:17:96:89 84:1b:5e:a8:bf:7f 82
1503251376.428309 84:1b:5e:a8:bf:7f 74:e2:f5:17:96:89 82
1503251376.478479 74:e2:f5:17:96:89 84:1b:5e:a8:bf:7c 28
1503251376.528461 74:e2:f5:17:96:89 84:1b:5e:a8:bf:7c 28
1503251377.059249 84:1b:5e:a8:bf:7f 74:e2:f5:17:96:89 106
1503251377.174706 84:1b:5e:a8:bf:7f 74:e2:f5:17:96:89 106
1503251377.879405 84:1b:5e:a8:bf:7f 74:e2:f5:17:96:89 100
1503251377.880309 84:1b:5e:a8:bf:7f 74:e2:f5:17:96:89 329
1503251377.880362 84:1b:5e:a8:bf:7f 74:e2:f5:17:96:89 106
中间空格由"\t" 隔开。
我需要做到的是,将第二行三行名字一样的全部提取出来并将对应的第四行汇总。
目前我已经可以将第二行所有不重复的名字提取出来。
我的问题是,我用了fopen打开的文件,用了fget读取的每一行,所以每一行都是字符串。我如何将第四行的数加起来。这里C会把它们当成string来处理,我如何将它们和对应的第二三列的名字加起来。
如何可以的话能否给我解释一下其中类型的变化? txt文件没打开之前数据是什么类型,用fget打开之后是不是都是字符串。 我如何将字符串变为int并放入二维数组对应相加减! 求助
下面是我需要达到的效果,和我目前写的代码。
题目要求用 /usr/bin/sort 和 fork()来完成最后的排序,我在网上搜了这两个。理解意思,但是真搞不懂如何用进去.
这是目前的结果
Ljc:Desktop killkelly214$ mycc -o test fopen1.c Ljc:Desktop killkelly214$ ./test 84:1b:5e:a8:bf:7f 00:8e:f2:c0:13:cc 84:1b:5e:a8:bf:7c 74:e2:f5:17:96:89 Ljc:Desktop killkelly214$
这是我想要的结果:
00:8e:f2:c0:13:cc 951
84:1b:5e:a8:bf:7f 929
84:1b:5e:a8:bf:7c 259
74:e2:f5:17:96:89 138
目前的代码 - #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int main (int argc, char *argv[]) //*argv[] pointer array
- {
- FILE *fp= fopen("sample-packets.txt","r");
- char line[BUFSIZ]; //建立一个很大的数组
- char copy[100][100]={{0}}; //准备将txt文件中的类容存入copy二维数组
- char tMac[50][50]={{0}}; //an array to collect the transmiter
- char rMac[50][50]={{0}};
- char stat[50][3]={{0}};
-
-
- int row=0;
- int column=0;
- int maxlength=0;
- int trl=0; // tMac row length
- int rrl=0; // rMac row length
- //fgets(array_name,n,file_name)
- // fgets, read a string from a specfied file to a array
-
- while( fgets(line, sizeof line, fp) != NULL)
- {
- int length = strlen(line);
- if(length > maxlength)
- {
- maxlength =length;
- }
- for (column=0;column<length;column++)
- {
- copy[row][column]=line[column];
- }
- row++;
-
- }
-
-
- int indexOft;
- int indexOfr;
- int indexOfs;
- for(int i=0; i < row;i++)
- { int j=0;
- int tcNum=0; // column number of tMac
- int rcNum=0; // column number of rMac
- int scNum=0; // column number of stat
- while(copy[i][j]!='\t')
- {
- j++;
- }
- indexOft = j;
- j+=1;
-
- while(copy[i][j]!='\t')
- {
- tMac[i][tcNum]=copy[i][j];
- j++;
- tcNum++;
- }
- trl=tcNum;
- j+=1;
- indexOfr=j;
-
- while(copy[i][j]!='\t')
- {
- rMac[i][rcNum]=copy[i][j];
- j++;
- rcNum++;
- }
- rrl=rcNum;
- j+=1;
- indexOfs=j;
-
- while(copy[i][j]!='\n')
- {
- stat[2-scNum][j]=copy[i][j];
- j++;
- scNum++;
- }
-
-
- }// end copying new array
- char dt[500][50]; //distinct array
- int dNum = 1; //distinct row number
-
- //the first row of dt is the first row of tMac;
-
- //pass the first row to distinct array
- for(int i = 0;i<trl;i++)
- {
- dt[0][i]=tMac[0][i];
- printf("%c",tMac[0][i]);
- }
-
- printf("\n");
- for(int i = 0; i<row; i++) //travesal each row
- {
- int identify = 1; //declare an number to show distinct row existed
- for(int e = 0;e < dNum;e++) //traversal each distinct row
- {
- identify=1;
- int deter[30]={0};
- for(int g=0;g<trl;g++) //to judge if the coming row is same as existed distinct row,
- {
- if(dt[e][g] == tMac[i][g])
- {
- deter[g] = 1;
- }
- }
- // fprintf(stderr, "I'm here %d %d\n", e, dNum); buffered-from your program to a buffer, but printf just print in the program.
- // stderr is a file pointer, stdout,
- for(int g=0;g<trl;g++)
- {
- if(deter[g] != 1)
- {
- identify = 0;
- }
- }
- if(identify == 1) //if the coming row is as same as existed row, traveral next row.
- {
- break;
- }
- }
- if(identify == 0) //if not the same, pass the coming row into distinct array, distinct array +1
- {
- dNum = dNum +1;
- for(int g = 0;g<trl;g++)
- {
- dt[dNum-1][g] = tMac[i][g];
- printf("%c",dt[dNum-1][g]);
- }
- printf("\n");
-
- }
-
-
- }
-
-
- fclose(fp);
- return 0;
- }
复制代码
[i][i][i]
求助,后面的数字应该如何加起来并且排序! 跪求帮助!
[/i][/i][/i] |