读取TXT文件并将值放入列表中(c++)
read a txt file and put the values in a list (c++)
文本文件中的值是这样的格式:
4
2 3
5 6
3 7
6 9
和输出必须看起来像这样:
[2:3][5:6][3:7][6:9]
这是我的代码:
#include <iostream>
#include <stdio.h>
using namespace std;
class node {
public:
int info;
node* next;
node(){
next = NULL;
}
node (int value){
info = value;
next = NULL;
}
};
class list {
private:
node* head;
public:
list() { //Constructor
head = NULL;
}
void insert(int value){
if (head == NULL){
head = new node;
head -> info = value;
return;
}
node *temp = head;
while (temp) {
temp = temp -> next;
}
temp -> next = new node;
temp -> info = value;
}
void showlist(){
node* temp = head;
temp = temp -> next; //ignore first number in txt file
cout << "Liste n" << endl;
while (temp){
printf ("%d", temp -> info);
//cout << temp -> info << endl;
temp = temp -> next;
}
}
~list() { //Destructor
node* temp = head;
while (head -> next != NULL) {
delete temp -> next;
temp -> next = NULL;
temp = temp -> next;
}
delete head -> next;
head -> next = NULL;
}
};
int main(int argc, const char * argv[]) {
list stone;
FILE* fp;
if (argc > 1)
fp = fopen(argv[1], "r");
else
fp = fopen("output.txt", "r");
if (!fp)
printf("Can't open file n");
else {
int value, state;
int i = 0;
do {
state = fscanf(fp, "%d", &value);
if (state != EOF){
stone.insert(value);
}
stone.showlist();
}
while (state != EOF);
fclose (fp);
}
return 0;
}
没有错误,但如果我想执行它,我得到了一个崩溃报告。
您的代码中有几个错误。我将列出几个明显的选项供您考虑:
- 你怎么处理头?在显示列表时忽略头部,但它实际上保存了第一个值。
- 每次你尝试使用
node->next
时,你应该保证节点不是NULL - 正如第一个答案所指出的,
while
循环后的温度为NULL。解可能为while (temp->next)
- 列表的析构函数不正确,空列表会遇到分段错误
- 您使用
printf
和fgets
代替iostream
,并且您使用两个类。你喜欢C还是c++ ?选择一个你喜欢的。
insert
中的这个块看起来不正确:
while (temp) {
temp = temp -> next;
}
temp -> next = new node;
在while
循环之后,temp
是NULL
,所以不能调用temp -> next
您似乎想要一个格式化的答案。但是你没有安排把它打印成
[2:3][5:6][3:7][完]
//THE MODIFICATIONS I MADE ARE MINUTE. BUT THEY'LL GIVE YOU THE RESULT YOU DESIRE.
// MODIFY TEXT FILE FOR ANY MORE INPUTS IF YOU NEED TO INSERT MORE
#include <iostream>
#include <stdio.h>
#include<conio.h>
using namespace std;
class node {
public:
int info;
node* next;
node(){
next = NULL;
}
node (int value){
info = value;
next = NULL;
}
};
class list {
private:
node* head;
public:
list() { //Constructor
head = NULL;
}
void insert(int value){
if (head == NULL){
head = new node;
head -> info = value;
return;
}
node *temp = head;
while (temp->next) { //MODIFIED INSERT LOOP TERMINATION CONDN
temp = temp -> next;
}
temp -> next = new node;
temp -> info = value;
}
void showlist(){
node* temp = head;
// temp = temp -> next; //ignore first number in txt file _ UNNECESSARY - COMMENTED
cout << "List n" << endl;
while (temp){
if(!temp->next){break;} //, temp -> info,(temp -> next)-> info);
else {printf ("[%d:%d]n", temp -> info,(temp -> next)-> info);}
//cout << temp -> info << endl;
temp = (temp -> next)->next;
}
}
~list() { //Destructor
node* temp = head;
while (head -> next != NULL) {
delete temp -> next;
temp -> next = NULL;
temp = temp -> next;
}
delete head -> next;
head -> next = NULL;
}
};
int main(int argc, const char * argv[]) {
list stone;
FILE* fp;
if (argc > 1)
fp = fopen(argv[1], "r");
else
fp = fopen("inp.txt", "r"); //CHANGED NAME OF FILE
if (!fp)
printf("Can't open file n");
else {
int value, state;
int i = 0;
do {
state = fscanf(fp, "%d", &value);
if (state != EOF){
stone.insert(value);
}
} while (state != EOF);
stone.showlist();
fclose (fp);
}
getch();
return 0;
}
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 一对向量构造函数:初始值设定项列表与显式构造
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过for循环使用用户输入填充列表
- C++:如何使函数只返回作为列表一部分的字符串
- 概念中的cv限定符需要表达式参数列表
- 下面是我为检测链接列表中的循环而制作的代码
- 建议在运行时将带有类实例的列表从c++导入qml
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在卡萨布兰卡形成编码参数的列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题