简体   繁体   English

来自文本文件的数组-不起作用

[英]Array from text file - not working

I am trying to create an array, with the data coming from a text file. 我正在尝试创建一个数组,数据来自文本文件。 I am then trying to count how many pieces of data there are in that array. 然后,我试图计算该数组中有多少数据。 In the array, there are two pieces of data, however when the counter executes, it says there is only 1. 在数组中,有两个数据,但是当计数器执行时,它说只有1个数据。

Here is my code: 这是我的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
   int n = 0;
   ifstream show_version_library;
   show_version_library.open("version_library.txt");
   char show_version_library_var[1024];
   if (show_version_library.is_open())
   {
       while (!show_version_library.eof())
       {
          show_version_library >> show_version_library_var;
       }
   }

   // string replace * to ,
   string show_version_library_actual( show_version_library_var );
   int position = show_version_library_actual.find( "*" ); // find first space
   while ( position != string::npos ) 
   {
       show_version_library_actual.replace( position, 1, "," );
       position = show_version_library_actual.find( "*", position + 1 );
   }  

   // string replace ^ to "
   string show_version_library_actual2( show_version_library_actual );
   int position2 = show_version_library_actual2.find( "^" ); // find first space
   while ( position2 != string::npos ) 
   {
       show_version_library_actual2.replace( position, 1, "\"" );
       position2 = show_version_library_actual2.find( "^", position2 + 1 );
   } 

   // convert show_version_library_actual2 to char*  
   char* lib2;
   lib2 = &show_version_library_actual2[0];

   // array counter
   char* library_actual[100] = {
                               lib2
                               };

   char* p;
   while (p != '\0')
   {
       n++;
       p = library_actual[n];
   }

   cout << "\nN is " << n << " that was n\n"; // should be outputting 2
   show_version_library.close();

   system("PAUSE");
   return 0;
}

(code has been reformatted so that it will show up as code here) (代码已重新格式化,因此将在此处显示为代码)

And in version_library.txt 并在version_library.txt中

"1.8_HACK"*"1.8"*

I tried outputting "lib2" and it comes up with 我尝试输出“ lib2”,它带有

"1.8_HACK", "1.8", 

.. as it should... .. 正如它应该...

However, when I output library_actual[0] , the whole line comes up, instead of just "1.8_HACK" . 但是,当我输出library_actual[0] ,整行显示,而不仅仅是"1.8_HACK"

I am fairly new to cpp, so please excuse my terrible code..if any. 我对cpp还是很陌生,所以请原谅我糟糕的代码。

Thank you! 谢谢!

I think things start going wrong from this section: 我认为本节开始出现问题:

   // convert show_version_library_actual2 to char*   
   char* lib2; 
   lib2 = &show_version_library_actual2[0]; 

That does not convert anything. 那不会转换任何东西。 What it does is declare a char pointer lib2 and sets it's value to the address of the first character in the show_version_library_actual2 char array. 它的作用是声明一个char指针lib2,并将其值设置为show_version_library_actual2 char数组中第一个字符的地址。 There's nothing wrong with that but no conversion is taking place. 没什么错,但是没有转换发生。

The sequence: 序列:

   // array counter 
   char* library_actual[100] = { 
                               lib2 
                               }; 

does not count anything. 什么都不算 What it does is setup an array of 100 pointers to char. 它要做的是设置一个由100个指向char的指针组成的数组。 You initialize it with only 1 value -- the pointer to the first character in the show_version_library_actual2 character array. 您仅用1个值进行初始化-指向show_version_library_actual2字符数组中第一个字符的指针。 The other 99 values in the array are uninitalized. 数组中的其他99个值是未初始化的。

The next loop: 下一个循环:

   char* p; 
   while (p != '\0') 
   { 
       n++; 
       p = library_actual[n]; 
   } 

probably isn't what you intended since it is dealing with a mostly uninitialized array. 可能不是您想要的,因为它处理的是大多数未初始化的数组。 It returns 1 but only because library_actual[0] (lib2 = first character of the show_version_library_actual2 array) is non null so the loop ends. 它返回1,但仅是因为library_actual [0](lib2 = show_version_library_actual2数组的第一个字符)不为null,所以循环结束。

I'm not sure exactly how you intended to identify the pieces of data. 我不确定您打算如何识别数据片段。 Maybe you thought that by adding commas that you'd get separate strings. 也许您认为通过添加逗号可以得到单独的字符串。 If that's what you wanted you have to remember that in C/C++ nothing like that would happen by itself. 如果那是您想要的,则必须记住,在C / C ++中,这种事情本身不会发生。 Assignments are usually just that -- no conversion is going to happen as might happen in many other languages. 分配通常就是这样-不会像许多其他语言那样发生转换。

If you need to get separate strings you'll need to do that. 如果需要获取单独的字符串,则需要这样做。 If you simply want to count the number of times a comma occurs that's fairly easy. 如果您只想计算逗号发生的次数,那将非常简单。 For example you could do: 例如,您可以这样做:

char *p = lib2;
int n = 0;
while(*p != '\0')
{
    if(*p == ',')
        ++n;
}

With this n will represent the number of comma. 以此n表示逗号数。 In your sample that would give you 2. I'll leave it as an exercise to you to handle the case where there's 2 items but separated by 1 comma. 在您的示例中,该示例将为您提供2。我将它作为练习留给您处理,其中有2个项目但由1个逗号分隔。 Hint: sometimes it's useful to add an initial or terminal separator to make processing consistent. 提示:有时添加初始或终端分隔符以使处理保持一致很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM