鱼C论坛

 找回密码
 立即注册
查看: 2930|回复: 12

C语言fopen的,fget,读取类型。

[复制链接]
回帖奖励 2 鱼币 回复本帖可获得 1 鱼币奖励! 每人限 5 次(中奖概率 30%)
发表于 2017-9-16 14:10:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

目前的代码
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>


  5. int main (int argc, char *argv[])   //*argv[] pointer array
  6. {
  7.     FILE *fp= fopen("sample-packets.txt","r");   

  8.     char line[BUFSIZ];  //建立一个很大的数组
  9.     char copy[100][100]={{0}};  //准备将txt文件中的类容存入copy二维数组
  10.     char tMac[50][50]={{0}}; //an array to collect the transmiter
  11.     char rMac[50][50]={{0}};
  12.     char stat[50][3]={{0}};
  13.    
  14.    
  15.     int row=0;
  16.     int column=0;
  17.     int maxlength=0;
  18.     int trl=0; // tMac row length
  19.     int rrl=0; // rMac row length

  20.     //fgets(array_name,n,file_name)
  21.     // fgets, read a string from a specfied file to a array
  22.    
  23.     while( fgets(line, sizeof line, fp) != NULL)
  24.     {
  25.         int length = strlen(line);
  26.         if(length > maxlength)
  27.         {
  28.             maxlength =length;
  29.         }
  30.         for (column=0;column<length;column++)
  31.         {
  32.             copy[row][column]=line[column];
  33.         }
  34.         row++;
  35.       
  36.     }
  37.    
  38.    
  39.     int indexOft;
  40.     int indexOfr;
  41.     int indexOfs;
  42.     for(int i=0; i < row;i++)
  43.     {   int j=0;
  44.         int tcNum=0; // column number of tMac
  45.         int rcNum=0; // column number of rMac
  46.         int scNum=0; // column number of stat
  47.         while(copy[i][j]!='\t')
  48.         {        
  49.                 j++;
  50.         }
  51.         indexOft = j;
  52.         j+=1;      
  53.         
  54.         while(copy[i][j]!='\t')
  55.         {
  56.             tMac[i][tcNum]=copy[i][j];
  57.             j++;
  58.             tcNum++;
  59.         }
  60.         trl=tcNum;
  61.         j+=1;
  62.         indexOfr=j;
  63.         
  64.         while(copy[i][j]!='\t')
  65.         {
  66.             rMac[i][rcNum]=copy[i][j];
  67.             j++;
  68.             rcNum++;
  69.         }
  70.         rrl=rcNum;
  71.         j+=1;
  72.         indexOfs=j;
  73.         
  74.         while(copy[i][j]!='\n')
  75.         {
  76.             stat[2-scNum][j]=copy[i][j];
  77.             j++;
  78.             scNum++;
  79.         }
  80.         
  81.         
  82.     }// end copying new array

  83.     char dt[500][50];   //distinct array
  84.     int dNum = 1; //distinct row number
  85.    
  86.     //the first row of dt is the first row of tMac;
  87.    
  88.     //pass the first row to distinct array
  89.     for(int i = 0;i<trl;i++)
  90.     {
  91.             dt[0][i]=tMac[0][i];
  92.             printf("%c",tMac[0][i]);
  93.     }
  94.    
  95.     printf("\n");

  96.     for(int i = 0; i<row; i++)   //travesal each row
  97.     {  
  98.             int identify = 1;  //declare an number to show distinct row existed

  99.         for(int e = 0;e < dNum;e++) //traversal each distinct row
  100.         {
  101.                 identify=1;
  102.                 int deter[30]={0};
  103.                 for(int g=0;g<trl;g++)  //to judge if the coming row is same as existed distinct row,
  104.                 {
  105.                         if(dt[e][g] == tMac[i][g])
  106.                         {        
  107.                                 deter[g] = 1;
  108.                         }
  109.                 }
  110.         //        fprintf(stderr, "I'm here %d %d\n", e, dNum);    buffered-from your program to a buffer, but printf just print in the program.
  111.         //        stderr is a file pointer, stdout,
  112.                 for(int g=0;g<trl;g++)
  113.                 {
  114.                         if(deter[g] != 1)
  115.                         {        
  116.                                 identify = 0;
  117.                         }
  118.                 }               
  119.                 if(identify == 1)  //if the coming row is as same as existed row, traveral next row.
  120.                 {
  121.                         break;
  122.                 }

  123.         }
  124.                 if(identify == 0) //if not the same, pass the coming row into distinct array, distinct array +1
  125.                 {
  126.                         dNum = dNum +1;

  127.                         for(int g = 0;g<trl;g++)
  128.                         {
  129.                                 dt[dNum-1][g] = tMac[i][g];
  130.                                 printf("%c",dt[dNum-1][g]);
  131.                         }
  132.                         printf("\n");
  133.                
  134.                 }
  135.                         
  136.    
  137.            }
  138.    
  139.    
  140.     fclose(fp);
  141.     return 0;
  142. }
复制代码


求助,后面的数字应该如何加起来并且排序! 跪求帮助!

txt文件数据

txt文件数据
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-16 14:55:29 | 显示全部楼层
不会C,水一下,用python
a = pd.read_table(r'D:\123.txt',names=list('abcd'))
a.pivot_table('d',index='b',aggfunc='sum').sort_valuse(by='d')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-16 15:16:22 | 显示全部楼层
边城 发表于 2017-9-16 14:55
不会C,水一下,用python
a = pd.read_table(r'D:\123.txt',names=list('abcd'))
a.pivot_table('d',ind ...

兄弟你别搞事好吧。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-16 16:15:48 | 显示全部楼层

回帖奖励 +1 鱼币

这里我没有用你的方式,下面是运行结果
C:/Users/Administrator/Desktop/1.png
  1. // TestFile.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include <string>
  5. #include <iostream>

  6. using namespace std;

  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9.         // 这里写的决对路径
  10.         FILE *fp = fopen("E:\\ProcedureTest\\EncryptAndDecrypt\\TestPrc\\TestFile\\Debug\\File.txt", "r");

  11.         // 建立数组
  12.         char line[BUFSIZ];  
  13.         string strTmac[50], strRmac[50];
  14.         int nStat[50] = { 0 };
  15.         int row = 0;

  16.         while (fgets(line, sizeof(line), fp) != NULL)
  17.         {
  18.                 string strLine(line);
  19.                 // 取出数据,分为三组,第二列,第三列,第四列
  20.                 int nIndex = strLine.find_last_of('\t');
  21.                 // 取第四列
  22.                 string strStat = strLine.substr(nIndex + 1, strLine.length() - nIndex - 2);       
  23.                 // 转换为整数
  24.                 nStat[row] = atoi(strStat.c_str());
  25.                 // 取第三列
  26.                 strLine = strLine.substr(0, nIndex);
  27.                 nIndex = strLine.find_last_of('\t');
  28.                 strRmac[row] = strLine.substr(nIndex + 1, strLine.length() - nIndex - 1);
  29.                 // 取第二列
  30.                 strLine = strLine.substr(0, nIndex);
  31.                 nIndex = strLine.find_last_of('\t');
  32.                 strTmac[row] = strLine.substr(nIndex + 1, strLine.length() - nIndex - 1);
  33.                 row++;
  34.         }

  35.         int nResult[50] = {0};
  36.         string strTResult[50];
  37.         string strRResult[50];
  38.         int nCount = 0;

  39.         for (int i = 0; i < row; i++)
  40.         {
  41.                 if (strTmac[i].empty())
  42.                 {
  43.                         continue;
  44.                 }
  45.                 for (int j = 0; j < row; j++)
  46.                 {
  47.                         // 除去自身
  48.                         if (i == j || strTmac[j].empty())
  49.                                 continue;
  50.                         // 判断二、三列是否相等
  51.                         if (strTmac[i] == strTmac[j] && strRmac[i] == strRmac[j])
  52.                         {
  53.                                 int k = 0;
  54.                                 bool bIsEqual = false;
  55.                                 for (; k < nCount; k++)
  56.                                 {
  57.                                         if (strTResult[k] == strTmac[j])
  58.                                         {
  59.                                                 nResult[k] += nStat[j];
  60.                                                 strTmac[j] = "";
  61.                                                 bIsEqual = true;
  62.                                         }
  63.                                 }
  64.                                 if (!bIsEqual)
  65.                                 {
  66.                                         // 说明保存结果里并没有
  67.                                         strTResult[nCount] = strTmac[i];
  68.                                         strRResult[nCount] = strRmac[i];
  69.                                         strTmac[j] = "";
  70.                                         nStat[i] += nStat[j];
  71.                                         nResult[nCount] = nStat[i];
  72.                                         nCount++;
  73.                                 }
  74.                         }
  75.                 }       
  76.         }
  77.         for (int i = 0; i < nCount; i++)
  78.         {
  79.                 cout << "TMac :" << strTResult[i] << " RMac:" << strRResult[i] << " value:" << nResult[i] << endl;
  80.         }
  81.         getchar();
  82.         return 0;
  83. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-16 16:18:58 | 显示全部楼层
上善若水··· 发表于 2017-9-16 16:15
这里我没有用你的方式,下面是运行结果

大神,不好意思我忘了。。题目要求必须用/usr/bin/sort 和 fork()这样来排序。。我真的搞不太明白
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-16 16:27:06 | 显示全部楼层

回帖奖励 +1 鱼币

Jc嘻嘻 发表于 2017-9-16 16:18
大神,不好意思我忘了。。题目要求必须用/usr/bin/sort 和 fork()这样来排序。。我真的搞不太明白

好吧,不过都差不多,处理方式都是一样的。不清楚字符与数字的区别,可以在网上找点资料看下。刚刚那个结果图没传上,再传一下! 1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-17 15:48:56 | 显示全部楼层
我也不会,不过顶楼主,祝你早日解决
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-17 15:57:19 | 显示全部楼层
谢谢楼主大大的分享
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-17 15:58:01 | 显示全部楼层

回帖奖励 +1 鱼币

if you study hard,you will make great progress
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-18 11:25:58 | 显示全部楼层
谢谢分享,哈哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-20 16:00:30 | 显示全部楼层
谢谢楼主分享
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-21 14:00:36 | 显示全部楼层
这么深奥的吗??
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-21 16:32:32 | 显示全部楼层
顶顶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 14:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表