【问题标题】:c++ by using argc and argv read the text filec++ 通过使用 argc 和 argv 读取文本文件
【发布时间】:2015-12-31 06:28:46
【问题描述】:
////////// new update!!!!! //////////

.txt 有 15 个数字,最后一个数字是“15” 1. 我尝试计算我的 .txt 文件中有多少(保存到我的索引中)数字。 2. 使用索引创建我的动态数组大小。 3. 将所有数字保存到我的动态数组中。

问题:如何将char动态数组覆盖为int动态数组。

我的终端输出垃圾:

Open sucessues!!  
index: 15
buffer: 15
0
1073741824
0
1073741824
2136670223
32767
-1680479188
32767
0
0
0
0
0
0
0

int main(int argc, char* argv[])
{

char *nPtr = argv[1];
char *buffer = new char[5];
int index = 0;



ifstream fin(nPtr); //open the file
if (argc > 1){

// allocate the memory
if(!fin){

cout << "can't read the file!!" << endl;  
return -1;
}

if(fin){

    cout << "Open sucessues!! " << endl;
}




while (!fin.eof()){

fin >> buffer;
index++; //counting here!!!
}

cout << "index: " << index << endl; //print out the counting results!
cout << "buffer: " << buffer << endl; // checking the last number!  should "15"

delete[] buffer; // 
buffer = NULL;

int *number = new int[index]; 
char *temp = new char[index];
int *home = number; //home

while(!fin.eof()){

    fin >> temp;
    *number= atoi(temp); //im confessing right here!!!
    number++;
    temp++;
}



number = home;

for (int i = 0; i < index; ++i)
{
    cout << *number << endl;  //*number print out garbage, i don't know why!
    number++;
}




fin.close( );
}

return 0;
}
/////************

//////// 旧的 ///////// 不读 ///// 我想知道如何使用 argc 和 argv 来读取文件: numbers.txt(few numbers里面)。 我的目标是:在终端中使用我的 ./sort 读取文件,例如: ./sort numbers 然后使用缓冲区和索引计算里面有多少个数字,使用索引创建动态数组,最后我再次读取文件,但使用atoi将所有“数字”更改为int。

我在终端中输入:./sort numbers。

有人可以帮我吗?我需要这些数组来对我的号码进行排序。 到这里为止:

int main(int argc, char* argv[])
{ 
    char *nPtr = argv[1]; 
    char *buffer[3];
    int index = 0;


    ifstream fin(nPtr); //open the file


    // allocate the memory
    if(fin.is_open()){


    cout << "open" << endl;

            while(!fin.eof()){
                fin >> *buffer;
                index++;



            }

    cout << index << endl;
    }

【问题讨论】:

  • 在索引到 argv 之前,您需要检查 argc。验证用户是否向您的程序提供了正确数量的参数。
  • 请不要添加无关的语言标签。爪哇?? C??
  • 您只为 buffer 分配了 3 个字符。如果fin 中有超过 3 个字符,您将覆盖缓冲区 -- 未定义行为(可能是崩溃)。
  • 写入取消引用未初始化指针导致分段错误

标签: c++


【解决方案1】:
char *buffer[3];

创建一个由三个指向字符的指针组成的数组。它不分配任何存储指向。它不分配任何要指向的存储。这些指针可能指向任何东西。有效记忆,无效记忆,你哥哥的色情藏匿处,你不知道。如果幸运的话,它们会指向无效的内存,你的程序就会崩溃。

fin >> *buffer;

尝试将从文件中读取的字符串放入上面三个指针中的第一个指向的内存中。因为我们不知道它们指向哪里,所以我们不知道文件的输入将被写入哪里。很有可能它会尝试写入无效内存,程序会崩溃。

为了解决这个问题,分配一些存储空间,将指针指向这个存储空间,然后读入指针。

例如。

char *buffer[3];
char storage[128];
buffer[0] = storage;

后来

fin >> *buffer;

也就是说,我认为这根本不是你想要的。更有可能

char *buffer[3];

应该是

char buffer[3];

在这种情况下

fin >> *buffer;

将从文件中读取一个字符到缓冲区,所以这也可能是一个错字和

fin >> buffer;

是什么意思。警告!!!如果从 fin 读取的字符串长于 2 个字符,这可能仍然会崩溃。一般来说,您可能需要重新考虑这一点。

如果允许,请使用 std::stringstd::vector,但鉴于现在还处于学期初期,您的老师可能希望通过让您用石头敲打东西并可能用树枝摩擦生火来教您编程。

【讨论】:

  • 你是这个世界上最好的,我想说我爱你
  • 嗨,这里还是有点麻烦。我在这里有 15 个索引,然后我输入了我的 int *number = new int[index];通过使用 while !fin.eof。如何使用 atoi 将所有字符串数转换为 int 数,我想先将其保存到缓冲区中, atoi(buffer);和 *number = 缓冲区。我可以这样做吗?
  • 我得到了这样的输出:打开成功!索引:15 0 -268435456 0 -268435456 2136670224 32767 -1680479189 32767 2136686792 32767 -1680479188 32767 0 0 0 xi
  • @stacker 使用新代码发布新问题。 2 个原因 1)添加更多的部分使这个问题变得混乱 2)我不知道你的代码现在是什么样子。
猜你喜欢
  • 1970-01-01
  • 2016-12-01
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
相关资源
最近更新 更多