2014-07-07 55 views
-1
void getAgentInfo(char** agent_address) 
{ 
    #define MAX_IP_SIZE 100 
    FILE *fcfg=NULL; 
    char line[MAXLINE]; 
    char *p , *pend; 
    int findlen_ips, findlen_num; 
    findlen_ips = strlen (findkey_ips); 
    findlen_num = strlen (findkey_num); 
    int iCount = 1; //in agent array entry 
    fcfg = fopen (FCFG, "r"); 

    while (p = fgets (line, MAXLINE, fcfg)) 
    { 
     //printf ("Looking at %s\n", p); 
     if (p = findval (line, findkey_num, findlen_num)) 
     { 
      pend = p + strlen (p) - 1; /* check last char for newline terminator */ 
      if (*pend == '\n') 
       *pend = 0; 
      printf ("%s\n", p); /* process/parse the value */ 
      NumOfIp = atoi(p); 
      //agent_address = (char*)calloc(NumOfIp + 1, sizeof(char)); 
      agent_address = new char*[NumOfIp + 1]; 
      for(int icount = 0; icount < NumOfIp+1; icount++) 
      { 
       agent_address[icount] = new char[MAX_IP_SIZE]; 
      } 
     } 

     if (p = findval (line, findkey_ips, findlen_ips)) 
     { 
      pend = p + strlen (p) - 1; /* check last char for newline terminator */ 
      if (*pend == '\n') 
       *pend = 0; 
      printf ("addr = %s\n", p); /* process/parse the value */ 
      agent_address[iCount]= p; //strcpy(agent_address[iCount], p) 
      //printf("agent_address[iCount++]=%s\n",agent_address[iCount]); 
      iCount++; 
     } 
    } 
} 

從上面的代碼中,我讀txt文件,並在INT VAR獲取值,並根據該值craeting二維字符串數組存儲值:分段故障而指定字符串

... 字符** agent_address = NULL; getAgentInfo(agent_address); TRACE_STR(「\ n」); agent_address [0] = MasterCPMAddr; ....

我成功從行agent_address[0] = MasterCPMAddr;得到字符串,但更確切地說,當我存儲它時,我得到分段錯誤; //用strcpy(agent_address[0], MasterCPMAddr)也嘗試

+0

也許你正試圖推動字符的字符串unallocatedd內存位置。 –

+1

在您調用'getAgentInfo'後,'agent_address'仍然是'NULL',因爲double指針是按值傳遞的。當你爲'代理地址'分配內存時,你必須通過引用來傳遞它(這會使它成爲'getAgentInfo'中的三重指針),或者可能更優雅地從'getAgentInfo'返回它。你是否應該分配一個字符串文字或者用'strcpy'複製字符取決於你的數據結構。 –

+0

明白了@MOehm ..謝謝... –

回答

1

note
您的代碼似乎是C,輕輕灑上C++。我會建議你選擇一邊。可以用C或C++編寫。後者提供了一個強大的庫,爲您處理文件IO和內存管理的實質。將std::vector<std::string>傳遞給你的函數在這裏是有意義的。更C++風格來解決你的問題是這樣的:

int read_lines(std::vector<std::string>& lines) 
{ 
    int count_lines = 0; 
    std::string line; 
    std::ifstream infile("file.txt"); 
    while (std::getline(infile, line)) 
    { 
     lines.push_back(line);//add line to vector 
     ++count_lines; 
    } 
    return count_lines; 
} 

一個完整的,經過測試和工作示例:

#include <iostream> 
#include <fstream> 
#include <vector> 

using namespace std;//to get rid of that pesky std:: 

int read_lines(vector<string>& lines) 
{ 
    int count_lines = 0; 
    string line; 
    ifstream infile("file.txt"); 
    while (getline(infile, line)) 
    { 
     lines.push_back(line);//add line to vector 
     ++count_lines; 
    } 
    return count_lines; 
} 

int main (void) 
{ 
    vector<string> list; 
    int lines_read = read_lines(list); 
    int i=1; 
    cout << "Read " << lines_read << " lines" << endl; 
    //from start to finish 
    for (vector<string>::iterator it = list.begin(); it != list.end(); ++it) 
     cout << "Line: " << i++ << " "<< *it << endl; 
    return 0; 
} 

如果你想這樣做,老式的C-方式,那麼這裏是我對您問題的回答:

您有未定義的行爲。在getAgentInfo函數內部,您有一個指向字符串的指針(ppend)。但是,這些指針只在函數中有效。您必須將實際字符串複製到agent_address變量(使用strcpystrncpy)。
確保您分配了存儲該字符串所需的內存,並告訴我們如何傳遞該變量。 ,一些字符串賦值給字符指針數組一個純粹的C函數可以是這個樣子:

int read_lines(char *store_lines[], size_t max_lines) 
{ 
    int i=0; 
    size_t len; 
    char buffer[200];//temp buffer 
    FILE *fp = fopen("file.txt", "r"); 
    if (fp == NULL)| 
     return -1;//error 
    while (i < max_lines && fgets(buffer, sizeof buffer, fp) != NULL) 
    {//read lines while we haven't reached the max, and there are lines to read 
     len = strlen(buffer); 
     store_lines[i] = malloc(len+1);//allocate memory 
     if (store_lines[i] == NULL) 
     { 
      fclose(fp);//ALWAYS fclose 
      return 0;//failed to allocate enough memory 
     } 
     *store_lines[i] = '\0';//set to empty 
     strncat(store_lines[i], buffer, len); 
     ++i;//next line 
    } 
    fclose(fp); 
    return i;//return number of lines read 
} 

你可以調用這個函數指針數組,像這樣:

char *data[10]; 
int check = read_lines(data, 10); 
if (check == -1) 
{ 
    fprintf(stderr, "unable to open file"); 
    exit(1); 
} 
if (check == 0) 
{ 
    fprintf(stderr, "failed to allocate memory\n"); 
    for (int i=0;data[i] != NULL;++i) 
     fprintf(stderr, "Read line %d: %s\n", i+1, data[i]);//allocated memory vs null-pointers, possible to free memory here 
    exit(1); 
} 
printf("Read %d lines out of %d\n", check, sizeof data/sizeof *data); 
for (int i=0;i<check;++i) 
{ 
    printf("Line %d: %s\n", i+1, data[i]); 
    free(data[i]); 
    data[i] = NULL; 
} 
return 0; 

如果你也希望read_lines函數也分配數組本身,你必須將指針的地址傳遞給指針(三層間接)。但爲了你自己,那你所愛的,避免任何可能:

int read_lines(char ***store_lines) 
{ 
    char buffer[200], 
     **target = *store_lines;//makes it easier 
    size_t len, i; 
    FILE *fp = fopen("file.txt", "r"); 
    if (fp == NULL) 
     return -1; 
    while(fgets(buffer, sizeof buffer, fp)) 
    { 
     len = strlen(buffer); 
     realloc(target, (1+i)*sizeof *target);//re-allocate memory for pointer array 
     if (target == NULL) 
     { 
      fclose(fp); 
      return 0; 
     } 
     target[i] = malloc(len+1);//allocate space for chars 
     if (target[i] == NULL) 
     { 
      fclose(fp); 
      return 0; 
     } 
     *target[i] = '\0';//empty string, enable strncat use 
     strncat(target[i], buffer, len); 
     ++i;//next line 
    } 
    fclose(fp); 
    return (int) i;//cast to int - optional 
} 

你可以調用這個函數像這樣:

char **data = NULL; 
int lines_read = read_lines(&data); 
if (lines_read == -1) 
{ 
    fprintf(stderr, "Could not open file"); 
    exit (EXIT_FAILURE); 
} 
if (lines_read == 0) 
{ 
    fprintf(stderr, "Not enough RAM"); 
    exit (EXIT_FAILURE); 
} 
for (int i=0;i<lines_read;++i) 
    printf("%d) %s\n", i, data[i]);//print out line-by-line 
//free memory 
1

在c中,參數是按值傳遞的。所以要通過agent_address給你的函數,你需要聲明它想:

void getAgentInfo(char*** agent_address) 
{ 

,然後解引用agent_address一個函數內。
現在,你的函數中的agent_address只是一個局部變量。

+0

'int&NumOfIp'在C中不起作用。通過引用傳遞一個C++事物 –

+0

是正確的。我的答案只涉及C,但是,OP將他的代碼C與C++混合在一起。 – macfij

+0

的確他是......我只是注意到了'新char *'位。 OP真的應該選擇,也許考慮使用'std :: string'類 –