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
函數內部,您有一個指向字符串的指針(p
和pend
)。但是,這些指針只在函數中有效。您必須將實際字符串複製到agent_address
變量(使用strcpy
或strncpy
)。
確保您分配了存儲該字符串所需的內存,並告訴我們如何傳遞該變量。 ,一些字符串賦值給字符指針數組一個純粹的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
也許你正試圖推動字符的字符串unallocatedd內存位置。 –
在您調用'getAgentInfo'後,'agent_address'仍然是'NULL',因爲double指針是按值傳遞的。當你爲'代理地址'分配內存時,你必須通過引用來傳遞它(這會使它成爲'getAgentInfo'中的三重指針),或者可能更優雅地從'getAgentInfo'返回它。你是否應該分配一個字符串文字或者用'strcpy'複製字符取決於你的數據結構。 –
明白了@MOehm ..謝謝... –