简体   繁体   English

用C ++将文件的一行写入两个不同的数组

[英]writing a single line of a file in C++ into two different arrays

suppose a text file has 11001100 11001101 假设一个文本文件具有11001100 11001101

and i open the text file and take the input from the file as pt[0]=11001100, pt[1]=11001101.. 我打开文本文件,并从文件中输入pt [0] = 11001100,pt [1] = 11001101。

but if i take the input from file as in>>pt it wont put it in two different arrays which is obvious but it takes the whole line . 但是如果我从>> pt中获取文件的输入,它将不会将其放置在两个不同的数组中,这很明显,但它占用了整行。 Thus I have to take another for loop and traverse through the whole string and when I find a null character i start putting into the second array . 因此,我必须进行另一个for循环并遍历整个字符串,当我发现一个空字符时,我便开始放入第二个数组。 But how can I do it without putting it into a variable and traversing .. I mean directly from the file itself .. 但是我怎么做而不将其放入变量并遍历..我的意思是直接从文件本身..

Read it one byte at a time (eg with fread()). 一次读取一个字节(例如,使用fread())。 Append each byte to pt[i], where i is incremented when a space is encountered. 将每个字节追加到pt [i],遇到空间时i在其中递增。

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

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