读取文本文件并将其放入数组中 [英] reading text file and put it in array

查看:112
本文介绍了读取文本文件并将其放入数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有这样的文字文件

date,aa,bb,cc,dd,ee,ff,r

21/02 / 2010,1020.2 ,23.1,33.3,312.47

i想把它放在数组中并用标签或空格替换逗号,

soo这就是我对文件进行排序的方法进入数组,但运行无法启动exe文件...停止后我得到错误

  #include   <   iostream  >  
#include < fstream >
#include < string >
使用 命名空间标准;

int main()
{
int arraysize = 100000 ;
int ary = 100000 ;
char myArray [arraysize] [ary];
char current_char;
int num_characters = 0 ;
int i = 0 ;
int b = 0 ;

ifstream myfile( file.txt);

if (myfile.is_open())
{
while (!myfile.eof())
{
myfile>> myArray的[I] [B];
i ++;
b ++;
num_characters ++;
}

for int i = 0 ; i< = num_characters; i ++)
{

cout<< myArray [i]<< \t [b];
}

system( pause);
}}



以及用空格替换昏迷的任何建议

i希望屏幕上的输出不在文件中
**文件非常大有大约3000行

解决方案

你不需要一个数组,你可以动态地做:< br $> b $ b

  #include   < span class =code-keyword><   iostream  >  
#include < fstream >
使用 命名空间标准;

int main()
{
ifstream fin( myfile.txt);

while (fin.good())
{
char c;
fin>> C;
cout<< (c == ' ,'' ':c);
}
}


如果你的线路多了100000怎么办?你需要养成一个有用的习惯来识别不可接受的代码;特别是,如果你必须有像100000这样的幻数,你就会做错事。



如果你使用的是C ++,那么使用C ++,而不是C,并使用标准C ++数据类型,例如 std :: vector std :: list

http://en.cppreference.com/w/cpp/container/vector [< a href =http://en.cppreference.com/w/cpp/container/vectortarget =_ blanktitle =New Window> ^ ],

< a href =http://en.cppreference.com/w/cpp/container/list> http://en.cppreference.com/w/cpp/container/list [ ^ ],

另见 http://en.cppreference.com/w/cpp/container [ ^ ]。



在您的情况下, std :: vector 将完全解决你的问题文件大小未知的问题。



如果需要,可以在文件完全读完后创建数组。



另一个问题,包括替换,可以使用另一个标准类 std :: string ,或者从 std :: basic_string :

http:// en .cppreference.com / w / cpp / string / basic_string [ ^ ],

http://en.cppreference.com/w / cpp / string [ ^ ]。



您可以使用

等方法http://en.cppreference.com/w/cpp/string/basic_string/copy [ ^ ],

http://en.cppreference.com/w/cpp/string/basic_string/erase [ ^ ],

http://en.cppreference.com/w/cpp/string/basic_string/insert [ ^ ],

http://en.cppreference.com/w/cpp/string/basic_string/replace [ ^ ]。



-SA


在我看来你正试图分配

a非常大的结构:一个100000 * 100的数组000字节



这可以防止程序启动,除非你有超过10GB的内存。

hi guys,i have text file like this
date,aa,bb,cc,dd,ee,ff,r
21/02/2010,1020.2,23.1,33.3,312.47
i want to put it in array and replace the comma "," with a tab or space
soo this is what i did to sort the file into array but i get error after running "cannot launch exe file ... stopped"

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
  int arraysize = 100000;
  int ary = 100000;
  char myArray[arraysize][ary];
  char current_char;
  int num_characters = 0;
  int i = 0;
  int b=0;

  ifstream myfile ("file.txt");

     if (myfile.is_open())
        {
          while ( !myfile.eof())
          {
                myfile >> myArray[i][b];
                i++;
                b++;
                num_characters ++;
          }      

 for (int i = 0; i <= num_characters; i++)
      {

         cout << myArray[i]<<"\t"[b];
      } 

      system("pause");
    }}


and any suggestions for replacing coma with space
i want the output on the screen not in a file
** the file is very large has about 3000 lines

解决方案

You don't need an array, you might do it on the fly:

 #include <iostream>
 #include <fstream>
using namespace std;

int main()
{
  ifstream fin("myfile.txt");

  while ( fin.good() )
  {
    char c;
    fin >> c;
    cout << (c == ',' ? ' ' : c);
  }
}


What if you have just a bit more lines as 100000? You need to develop a useful habit to recognize unacceptable code; in particular, if you have to have any "magic number" like 100000, you are doing something wrong.

If you are using C++, use C++, not C, and use standard C++ data type, such as std::vector or std::list:
http://en.cppreference.com/w/cpp/container/vector[^],
http://en.cppreference.com/w/cpp/container/list[^],
see also http://en.cppreference.com/w/cpp/container[^].

In your case, std::vector will fully resolve your problem with unknown file size.

If you want, you can create an array after your file is fully read.

The other problem, with replacements, can be solved using another standard class, std::string, or one of the classes derived from std::basic_string:
http://en.cppreference.com/w/cpp/string/basic_string[^],
http://en.cppreference.com/w/cpp/string[^].

You can use methods like
http://en.cppreference.com/w/cpp/string/basic_string/copy[^],
http://en.cppreference.com/w/cpp/string/basic_string/erase[^],
http://en.cppreference.com/w/cpp/string/basic_string/insert[^],
http://en.cppreference.com/w/cpp/string/basic_string/replace[^].

—SA


It seems to me you are trying to allocate
a very big structure : an array of 100000 * 100000 bytes
= 10,000,000,000 bytes, near 10GB.

This prevents the program to start, unless you have much more than 10GB of ram.


这篇关于读取文本文件并将其放入数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆