g ++中的分段错误和从文件中读取
Segmentation Fault in g++ and reading from files
这个程序有问题,它从两个文件中读取。
每个文件 A 和 B 在顶部都包含一个 int,它是它所拥有的字符串的数量。程序要做一个B-A操作,文件A有2个字符串,"two"和"three"。文件 B 有 4 个字符串,"one"、"two"、"three" 和 "four"。所以第三个文件中的结果必须是 "one" 和 "four"。那些不在文件A中,但在文件B中的。
首先我读取了每个文件的大小,然后是每个文件的字符串。
我计算结果文件的大小并比较两个文件的字符串。
我使用了 gdb 调试器,它说问题出在比较文件的 for 中。我所做的是:
如果 B 中的字符串在 A 中找不到,我将其放入结果中。
我把整个程序写在这里(不是太大)所以你可以得到一个更好的主意。
非常感谢和抱歉变量中的西班牙名称。
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
int contador = 0;
string* datosA;
string* datosB;
string* resultado;
int tamA, tamB, tamResultado;
ifstream fichA("fA.txt");
if(fichA){
fichA>> tamA;
datosA = new string [tamA];
for(int i = 0; i < tamA; i++){
fichA>> datosA[i];
}
fichA.close();
}
ifstream fichB("fB.txt");
if(fichB){
fichB>> tamB;
datosB = new string [tamB];
for(int i = 0; i < tamB; i++){
fichB>> datosB[i];
}
fichB.close();
}
tamResultado = tamB - tamA;
resultado = new string [tamResultado];
contador = 0;
for(int i = 0; i < tamB; i++){
bool enc = true;
for(int j = 0; j < tamA && enc; j++){
if(datosB[i] != datosA[j]){
enc = false;
}
}
if(!enc){
resultado[contador] = datosB[i];
contador++;
}
}
delete[] datosA;
delete[] datosB;
ofstream fout("resultado.txt");
if(fout){
for(int i = 0; i < tamResultado; i++){
fout<< resultado[i];
}
fout.close();
}
delete[] resultado;
datosA = datosB = resultado = 0;
return 0;
}
检查你的条件。
当你遇到不同的元素时,你正在改变你的旗帜。但只有当数组 A 中的所有元素都与数组 B 中特定位置的元素不同时才会发生这种情况。
按照现在的写法,您正试图将数组 B 中的所有四个元素添加到生成的数组中,这是分段错误,因为您正试图访问未分配的内存。
一个可能的解决方案是添加计数器来检查数组 A 中的两个元素是否与数组 B 中的元素不同。
像这样:
for(int i = 0; i < tamB; i++){
bool enc = true;
// counter that keeps track of number of different elements
int count = 0;
for(int j = 0; j < tamA && enc; j++){
if(datosB[i] != datosA[j]){
count++;
}
}
// check if all elements in datosA are different than datosB[i]
if(count == tamA){
resultado[contador] = datosB[i];
contador++;
} }
这应该可以正常工作。
这个程序有问题,它从两个文件中读取。 每个文件 A 和 B 在顶部都包含一个 int,它是它所拥有的字符串的数量。程序要做一个B-A操作,文件A有2个字符串,"two"和"three"。文件 B 有 4 个字符串,"one"、"two"、"three" 和 "four"。所以第三个文件中的结果必须是 "one" 和 "four"。那些不在文件A中,但在文件B中的。
首先我读取了每个文件的大小,然后是每个文件的字符串。 我计算结果文件的大小并比较两个文件的字符串。
我使用了 gdb 调试器,它说问题出在比较文件的 for 中。我所做的是: 如果 B 中的字符串在 A 中找不到,我将其放入结果中。
我把整个程序写在这里(不是太大)所以你可以得到一个更好的主意。
非常感谢和抱歉变量中的西班牙名称。
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
int contador = 0;
string* datosA;
string* datosB;
string* resultado;
int tamA, tamB, tamResultado;
ifstream fichA("fA.txt");
if(fichA){
fichA>> tamA;
datosA = new string [tamA];
for(int i = 0; i < tamA; i++){
fichA>> datosA[i];
}
fichA.close();
}
ifstream fichB("fB.txt");
if(fichB){
fichB>> tamB;
datosB = new string [tamB];
for(int i = 0; i < tamB; i++){
fichB>> datosB[i];
}
fichB.close();
}
tamResultado = tamB - tamA;
resultado = new string [tamResultado];
contador = 0;
for(int i = 0; i < tamB; i++){
bool enc = true;
for(int j = 0; j < tamA && enc; j++){
if(datosB[i] != datosA[j]){
enc = false;
}
}
if(!enc){
resultado[contador] = datosB[i];
contador++;
}
}
delete[] datosA;
delete[] datosB;
ofstream fout("resultado.txt");
if(fout){
for(int i = 0; i < tamResultado; i++){
fout<< resultado[i];
}
fout.close();
}
delete[] resultado;
datosA = datosB = resultado = 0;
return 0;
}
检查你的条件。
当你遇到不同的元素时,你正在改变你的旗帜。但只有当数组 A 中的所有元素都与数组 B 中特定位置的元素不同时才会发生这种情况。
按照现在的写法,您正试图将数组 B 中的所有四个元素添加到生成的数组中,这是分段错误,因为您正试图访问未分配的内存。
一个可能的解决方案是添加计数器来检查数组 A 中的两个元素是否与数组 B 中的元素不同。
像这样:
for(int i = 0; i < tamB; i++){
bool enc = true;
// counter that keeps track of number of different elements
int count = 0;
for(int j = 0; j < tamA && enc; j++){
if(datosB[i] != datosA[j]){
count++;
}
}
// check if all elements in datosA are different than datosB[i]
if(count == tamA){
resultado[contador] = datosB[i];
contador++;
} }
这应该可以正常工作。