[英]How to rectify segmentation fault?
程序下面顯示錯誤:“ segmentation fault(core dumped)”。
它應該使用vector顯示平面文件記錄,讀取記錄的每一行並獲取該字段的記錄,然后推回到vector r_record。 然后推回桌子。
//.. includes
#define LIMIT 72
#define FIELD 25
using namespace std;
typedef vector <string> record_t;
typedef vector <record_t> table_t;
char line[FIELD];
string s_field;
table_t table;
record_t r_record;
void getField(char s[LIMIT])
{
char field[LIMIT];
int i=0;
r_record.clear();
while(s[i] != '\n')
{
if (s[i] != '\t' )
{
field[i] = s[i];
//*s_field = *s_field+1;
}
i++;
}
s_field = field;
r_record.push_back(s_field);
}
void getLine(FILE *fp)
{
char c;
int j=0;
table.clear();
l1:while ( (c = getc(fp)) != EOF )
{
if( c != '\n' )
{
line[j] = c;
}
j++;
}
getField(line);
table.push_back(r_record);
if(c != EOF)
goto l1;
cout<<table.size();
}
int main()
{
double total_time;
clock_t start, end;
start = clock();//time count starts
FILE *f1;
f1 = fopen("somesamplefile.txt","r+");
getLine(f1);
end = clock();//time count stops
total_time = ((double) (end - start)) / CLOCKS_PER_SEC; //calulate total time
printf("\nTime taken to print %f\n", total_time);
return 0;
}
我將關注@MattMcNabb所發表的有關在將值分配給字符串中的各個字符之前檢查限制的評論。
此外,您還沒有在getField
中將終止field
getField
null。 沒有那個,聲明
s_field = field;
可能會超出范圍訪問數據並導致未定義的行為。
void getField(char s[LIMIT])
{
// **** CORRECTION *****
// I am guessing you meant to use field[FIELD].
// char field[LIMIT];
char field[FIELD];
int i=0;
r_record.clear();
// **** CORRECTION *****
// Add a check to stop going beyond the limits of the array.
// while(s[i] != '\n')
while( i < FIELD-1 && s[i] != '\n')
{
if (s[i] != '\t' )
{
field[i] = s[i];
//*s_field = *s_field+1;
}
i++;
}
// **** CORRECTION *****
// Add this
field[i] = '\0';
s_field = field;
r_record.push_back(s_field);
}
另外,我想你打算用
char line[LIMIT];
代替
char line[FIELD];
衷心001 25 city1衷心002 26 city1衷心003 27 city1衷心004 25 city1衷心005 28 city1衷心006 29 city1衷心007 30 city1衷心001 25 city1衷心002 26 city1衷心003 27 city1
在文本中具有制表符分隔格式,應顯示不正確
校正后的程序
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdio>
#include <ctime>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <sys/stat.h>
#include <iterator>
#include <exception>
#include <dirent.h>
#include <unistd.h>
#include <map>
#include <unordered_map>
#include <sys/timeb.h>
#include <stdexcept> // std::out_of_range
#include <time.h>
#define LIMIT 22
#define FIELD 8
using namespace std;
typedef vector <string> record_t;
typedef vector <record_t> table_t;
char line[LIMIT];
string s_field;
table_t table;
record_t r_record;
vector <string>::iterator t_record;
void getField(char s[LIMIT])
{
// **** CORRECTION *****
// I am guessing you meant to use field[FIELD].
// char field[LIMIT];
char field[FIELD];
int i=0;
r_record.clear();
// **** CORRECTION *****
// Add a check to stop going beyond the limits of the array.
// while(s[i] != '\n')
while( i < FIELD-1 && s[i] != '\n')
{
if (s[i] != '\t' )
{
field[i] = s[i];
//*s_field = *s_field+1;
}
i++;
}
// **** CORRECTION *****
// Add this
field[i] = '\0';
s_field = field;
r_record.push_back(s_field);
}
void getLine(FILE *fp)
{
char c;
int j=0;
table.clear();
l1:while ( j < LIMIT-1 )
{
c = getc(fp);
if( c != '\n' )
{
line[j] = c;
j++;
}
}
line[j] = '\0';
if( c!= EOF )
{
getField(line);
table.push_back(r_record);
j=0;
goto l1;
}
}
int main()
{
double total_time;
clock_t start, end;
start = clock();//time count starts
FILE *f1;
f1 = fopen("somesamplefile.txt","r+");
getLine(f1);
for(t_record = r_record.begin(); t_record != r_record.end(); ++t_record )
{
cout <<*t_record <<endl;
}
cout<<table.size();
end = clock();//time count stops
total_time = ((double) (end - start)) / CLOCKS_PER_SEC; //calulate total time
printf("\nTime taken to print %f\n", total_time);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.