需要C -urgent帮助中的文件​​处理! [英] File handling in C -urgent help required!!!

查看:89
本文介绍了需要C -urgent帮助中的文件​​处理!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i有一个文件.txt如下,这将是一个输入文件



#test

attr1

attr2

attr3



i想要读取文件并将所有这些属性保存在使用C的向量或数组。

并且需要迭代向量并附加几个字符串并将唯一的属性写入文件(或保存在某个位置,硬编码位置没关系

i是编程的初学者。感谢您的帮助。







o / p file将如下



attr1 = somevalu1

attr2 = somevalue2

..







请急急!!!

Hi ,
i have a file .txt as below,which will be a input file

#test
attr1
attr2
attr3

i want to read the file and keep all these attributes in a vector or array using C.
and need to iterate through the vector and append few string and write the only the attributes to a file (or save in some location,hard coding location is okay
i am a beginner to programming.Appreciate for your help.



o/p file will be as below

attr1 = somevalu1
attr2 = somevalue2
..



please help urgent!!!

推荐答案

否这并不紧急。您可以通过以下任何链接了解文件处理: HTTPS://www.google.c o / webhp?sourceid = chrome-instant& ion = 1& espv = 2& ie = UTF-8#q = readfile%20c [ ^ ]。
No it's not urgent. And you can learn about file handling at any of the following links: https://www.google.co/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=readfile%20c[^].


请参阅:

https:// www.cs.bu.edu/teaching/c/file-io/intro [ ^ ],

http: //en.wikipedia.org/wiki/C_file_input/output [ ^ ]。



请注意,C数组的大小是固定的。即使你可以调整向量的大小,这实际上是重新分配,这是不合理的效率:

http ://randu.org/tutorials/c/dynamic.php [ ^ ],

http ://forum.codecall.net/topic/51010-dynamic-arrays-using-malloc-and-realloc [ ^ ]。



因此,它将是更好地更改文件格式并在第一行中写入记录数(行数,无论它是什么),以及您可能需要的所有其他元数据。这种前缀方法效率更高,因为你可以在一开始只分配一次数组内存。



参见:http://en.wikipedia.org/wiki/Metadata [ ^ ]。



-SA
Please see:
https://www.cs.bu.edu/teaching/c/file-io/intro[^],
http://en.wikipedia.org/wiki/C_file_input/output[^].

Note that C arrays are of fixed size. Even though you can resize the vector, this is actually reallocation, which is not reasonable efficient:
http://randu.org/tutorials/c/dynamic.php[^],
http://forum.codecall.net/topic/51010-dynamic-arrays-using-malloc-and-realloc[^].

Therefore, it would be much better to change the file format and write the number of records (lines, whatever it is) in first line, as well as all other metadata you may need. This "prefix" approach is much more efficient, as you can allocate array memory only once, in the very beginning.

See also: http://en.wikipedia.org/wiki/Metadata[^].

—SA


这篇关于需要C -urgent帮助中的文件​​处理!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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