C ++:从空字符的文件读取 [英] C++: reading from a file with null characters

查看:149
本文介绍了C ++:从空字符的文件读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我必须从一个文件中读取数据,而不是空格分隔不同的字段有空字符。当从文件中获取整数时,它们被提取出来,但是对于字符串,我只是从未初始化的字符数组中获得一个空格和垃圾。任何想法如何只提取字符到我的字符数组忽略空字符。

编辑:
$ b $

  char fName [15],lName [15 ],pMethod [5],roomType [10],目的[15]; 

int days,roomNum;

长的guestID;

datafile>> guestID;
datafile.getline(fName,15,'\0');
datafile.getline(lName,15,'\0');

cout<< guestID<< endl;
cout<< fName<< endl;
cout<< lName<< endl;

是我现在使用的代码,不幸的是fName不是再次抓取除null之外的任何东西
和lName获取fName的字符串值。正在考虑只是将字符串转换为字符串并将其转换。

解决方案

使用 getline 并通过 \0 (空字符)作为分隔符。


I have to read data from a file for an assignment unfortunately instead of spaces separating the various fields there are null characters. When taking integers from the file they are extracted fine however with the strings i just get a blanks space and garbage from my uninitialized character array. Any ideas as how to just extract the characters into my character array ignoring the null characters.

EDIT:

char fName[15],lName[15],pMethod[5],roomType[10],purpose[15];

int days, roomNum;

long guestID;

datafile>>guestID;
datafile.getline(fName,15,'\0');
datafile.getline(lName,15,'\0');

cout<<guestID<<endl;
cout<<fName<<endl;
cout<<lName<<endl;

is the code I'm using now unfortunately fName isnt grabbing anything other than null again and lName is getting fName's string value. Was thinking about just getting the numbers as string and converting them.

解决方案

Use getline and pass \0 (null character) as the delimiter.

这篇关于C ++:从空字符的文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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