c ++ dot net读取文本文件?

时间:2014-07-17 08:04:12

标签: .net c++-cli

StreamReader ^ sr1=File::OpenText("test\\powerline.txt");
try
{

        String^ s="";
        while (s=sr->ReadLine())
        {
            power_line=s->Split(':');
            power_line_nospace=power_line[1]->Split(' ');       
            d_line=power_line_nospace[1]->Split('|');
            for(int a=0;a<d_line->Length;a++)
            {
                pin_ptr<const wchar_t> wch = PtrToStringChars(d_line[a]);
                size_t origsize = wcslen(wch) + 1;
                size_t convertedChars = 0;
                if(a==0)
                {
                    wcstombs_s(&convertedChars, power_list[i].name_1, origsize, wch, _TRUNCATE);

                }
                if(a==1)
                {
                    wcstombs_s(&convertedChars, power_list[i].Max_1, origsize, wch, _TRUNCATE);

                }
                if(a==2)
                {
                    wcstombs_s(&convertedChars, power_list[i].Min_1, origsize, wch, _TRUNCATE);

                }

            }
            i++;

        }



        Console::WriteLine(s);
    }
    finally
    {
        if(sr)
            delete (IDisposable^)(sr);
    }

我的文本文件格式:

ABCDEFG Report (01-07-2014)  <---this is title;

222|ID 045: pli-dty|114.028934494208|22.4480290858776

353|ID 045: pli-dty|114.02891866233|22.4480086173862

484|ID 045: pli-dty|114.02891866233|22.4480086173862

525|ID 045: pli-dty|114.02886935268|22.4479448442396

662|ID 045: pli-dty|114.028861009955|22.4479340181094

869|ID 045: pli-dty|114.028860146491|22.4479329788007

第一行(标题)怎么不读?

2 个答案:

答案 0 :(得分:0)

String^ str = System::IO::File::ReadAllText("filepath/filename.txt");

答案 1 :(得分:0)

我认为拆分行是导致问题的原因,因为第一行不包含:

power_line=s->Split(':'); // power_line will contain just one element [0] for the first line of the file
power_line_nospace=power_line[1]->Split(' ');  // This should throw an IndexOutOfRangeException for the first line