LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Writing and Reading ASCII-File

We want to write an application where the errors (and information) occurred are stored in an ASCII file in a specific format with delimiter between the strings in a line for each. Can we read and write from the file simultanously? Displaying the file (size about 1MB) in a table control is slow, because we have to break the line into tokens an then show them. What is the best way to display the file?
0 Kudos
Message 1 of 2
(3,251 Views)
If by "read and write from file simultaneously" you mean read a record then write a record (or vice-versa) without opening and closing the file, the answer is yes.
Look at the fopen r+ and w+ options.
To open an existing file for reading and writing, use "r+".
To create a new file (or overwrite an existing file), use "w+".
If you design your file using fixed length records, you can define the record structure using a struct, and then you don't have to "break the line into tokens": just pass the structure to fread() or fwrite() and they'll do it for you.
Are you trying to create a spreadsheet? Have you looked at using CVI to call Excel to display your errors? Then you'd get all the functions built into Excel (like searching, sorting, replacing, formatting and
printing...). If you're trying to display a 1Mb table, if would be handy to do more than scroll through it. CVI ships with Excel demo programs.
Ignoring your file size for a minute, attached is a simple example of random file IO using fixed length records. It includes a sample data file with three records (Error 1, Error 2, and Error 3). It uses a struct to define the record structure and fread and fwrite to read and write. Writes (in this simple example) are always appended.
0 Kudos
Message 2 of 2
(3,251 Views)