Click here to Skip to main content
15,569,858 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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!!!
Posted
Updated 10-Mar-15 11:46am
v2
Comments
Sergey Alexandrovich Kryukov 10-Mar-15 16:47pm    
Vector? Did you mean C++ then, not C? :-)
—SA

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[^].
 
Share this answer
 
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900