c语言文件getline返回值,如何使用std :: getline()将文本文件读入C中的字符串数组?...

我试图在我的项目中使用std :: getline()将文本文件读入字符串数组.

这是我的代码:

ifstream ifs ( path );

string * in_file;

int count = 0;

while ( !ifs.eof() )

{

++count;

if ( count == 1 )

{

in_file = new string[1];

}

else

{

// Dynamically allocate another space in the stack

string *old_in_file = in_file;

in_file = new string[count];

// Copy over values

for ( int i = 0 ; i < ( count - 1 ) ; i++ )

{

in_file[i] = old_in_file[i];

}

delete[] old_in_file;

}

// After doing some debugging I know this is the problem what am I

// doing wrong with it?

getline(ifs,in_file[count - 1]);

}

所以在做了一些解码后我知道getline()没有在字符串数组中放置任何值.它似乎在数组中放置一个空字符串.

目标是读取文本文件,如:

Hello

Bye

See you later

该数组将被填充如下:

in_file [0] = Hello

in_file [1] = Bye

in_file [2] = See you later

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值