C Program - Build an index file from another file - c

I have a .bin file that contains records in CSV format what I want to do is assign each record in the bin file a sequence number. So the first record in the bin file would be assigned 0 and so on. These will be placed into a bianry index file such as (username, seq #).
If I have the bin file already created with records in it, how do I go through the bin file and index each record? Thanks for any help!

You would read lines from file A, and write lines to file B. Keep a count of each line you read and use that to generate the sequence number column when writing to file B.
To be honest, I would probably use Excel or a spreadsheet app if the file is already in CSV format

Related

I want to use sytem call write() without deleting data from the file [duplicate]

So I have a txt file that looks like this:
112 12.50 Y 15
267 7.75 N 20
382 15.50 N 45
User is prompted where he wants to insert a new ID. My job is to make the program write the new ID into the .txt file without totally overwriting all the information. Say the user wanted to insert the new ID after 267. The user tells me new ID is 123, 12.34, N, 12. The .txt file would have to look like this:
112 12.50 Y 15
267 7.75 N 20
123 12.34 N 12
382 15.50 N 45
In standard C there's no functionality to insert new data at a certain location within a file.
The only two options in plain C are:
Create a temporary file, copy the old file's data up to the insertion point to the temp file, write the new data to the temp file, copy the rest of the old file's data to the temp file, rename the temp file to the old file's name.
Figure out how much new data needs to be inserted, move (by copying) all data from the insertion point by that amount, write the new data at the insertion point.
There may be OS-specific functions to perform insertion of data at an arbitrary location within a file. But, again, not in the standard C library as defined by the C standard.
The only option that you have to add information in the middle of a file without overwriting old data is to move manually all the data following the position where you want to add into the file.
steps:
Create a temporary file
Read each line from the source file and write to temporary file, parsing it as you go
Then insert the new line after you've found the ID after which you would want the new line to be inserted
write all the remaining lines from the source file
delete the source file
rename the temporary file to the name of the source file.
celebrate!

Read matrices from multiple .csv files and print matrices in .csv files

So I have to write a C program to read data from .csv files supplied to me by multiple users, into matrices on which I will perform some operations (like matrix addition, multiplication with necessary conditions on dimensions, etc.) and print these matrices (or the output data) in to .csv files again.
I also need to dynamically allocate memory to my matrices.
Now, I have zero background in dealing with .csv files. I do not at all know the required code to read a .csv file or write into a .csv file. I have searched for long on the Internet but surprisingly I have not found any program that teaches how to deal with .csv files from the elementary level.
I am lost on this and need a lot of guidance, maybe a sample, fully well-written C program as I need a comprehensive example to begin with.
A CSV file is just a plain ASCII text file that contains a grid of values. Think of the file as a set of rows in a database table where each line in the file represents one record and the order of the data in each line is identical. Each item of data is separated using a comma character (hence the name). So to read the file:-
open file
until the end of the file
read line into a string
split the string into sub strings where ',' is the dilimiter
parse each sub string
Since there is no formatting information in a CSV file, if the data in each value consists of a string, then what do you do if the value has a comma in it? For reading numbers that is not a problem for you.
You could read the file in several passes, the first to determine the amount of data there is (number of columns, number of rows, etc) and the second to actually read the data.
Writing the CSV is quite simple:-
open file
for each record to write
for each element to write
write element
if not last element
write a comma
write a new line

What do the numbers in an archive file mean?

I'm using the ar command in Unix to create an archive file. In the resulting archive file each item in the file starts with something like
main.c/ 1382296179 49083 12433 100660 1256 `
(followed by the contents of main.c)
What does the long list of numbers mean?
Thanks!
The ar format is not standardized, but, according to Wikipedia, the file header format can usually be expected to contain the following fields, which appear to match your example:
File name
File modification time
Owner ID number
Group ID number
File mode in octal
File size
Grave accent (`) and linefeed

Appending text information to a text file at a certain spot without overwriting old information (C)

So I have a txt file that looks like this:
112 12.50 Y 15
267 7.75 N 20
382 15.50 N 45
User is prompted where he wants to insert a new ID. My job is to make the program write the new ID into the .txt file without totally overwriting all the information. Say the user wanted to insert the new ID after 267. The user tells me new ID is 123, 12.34, N, 12. The .txt file would have to look like this:
112 12.50 Y 15
267 7.75 N 20
123 12.34 N 12
382 15.50 N 45
In standard C there's no functionality to insert new data at a certain location within a file.
The only two options in plain C are:
Create a temporary file, copy the old file's data up to the insertion point to the temp file, write the new data to the temp file, copy the rest of the old file's data to the temp file, rename the temp file to the old file's name.
Figure out how much new data needs to be inserted, move (by copying) all data from the insertion point by that amount, write the new data at the insertion point.
There may be OS-specific functions to perform insertion of data at an arbitrary location within a file. But, again, not in the standard C library as defined by the C standard.
The only option that you have to add information in the middle of a file without overwriting old data is to move manually all the data following the position where you want to add into the file.
steps:
Create a temporary file
Read each line from the source file and write to temporary file, parsing it as you go
Then insert the new line after you've found the ID after which you would want the new line to be inserted
write all the remaining lines from the source file
delete the source file
rename the temporary file to the name of the source file.
celebrate!

Read/Write files in C

I'm writing a program in C that basically creates an archive file for a given list of file names. This is pretty similar to the ar command in linux. This is how the archive file would look like:
!<arch>
file1.txt/ 1350248044 45503 13036 100660 28 `
hello
this is sample file 1
file2.txt/ 1350512270 45503 13036 100660 72 `
hello
this is sample file 2
this file is a little larger than file1.txt
But I'm having difficulties trying to exract a file from the archive. Let's say the user wants to extract file1.txt. The idea is it should get the index/location of the file name (in this case file1.txt), skip 58 characters to reach the content of the file, read the content, and write it to a new file. So here's my questions:
1) How can I get the index/location of the file name in the archive file? Note that duplicate file names are NOT allowed, so I don't have to worry about having two different indecies.
2) How can I skip several characters (in this case 58) when reading a file?
3) How can I figure out when the content of a file ends? i.e. I need it to read the content and stop right before the file2.txt/ header.
My approach to solving this problem would be:
To have a header information that contains the size of each file, its name and its location in the file.
Then parse the header, use fseek() and ftell() as well as fgetc() or fread() functions to get bytes of the file and then, create+write that data to it. This is the simplest way I can think of.
http://en.wikipedia.org/wiki/Ar_(Unix)#File_header <- Header of ar archives.
EXAMPLE:
#programmer93 Consider your header is 80 bytes long(header contains the meta-data of the archive file). You have two files one of 112 bytes and the other of 182 bytes. Now they're laid out in a flat file(the archive file). So it would be 80(header).112(file1.txt).182(file2.txt).EOF . Thus if you know the size of each file, you can easily navigate(using fseek()) to a particular file and extract only that file. [to extract file2.txt I will just fseek(FILE*,(112+80),SEEK_SET); and then fgetc() 182 times. I think I made myself clear?
If the format of the file cannot be changed by adding additional header information to help, you'll have to search through it and work things out as you go.
This should not be too hard. Just read the file, and when you read a header line such as
file1.txt/ 1350248044 45503 13036 100660 28 `
you can check the filename and size etc. (You know you'll have a header line at the start after the !<arch>). If this is the file you want, the ftell() function from stdio.h will tell you exactly where you are in the file. Since the file size in bytes is given in the header line, you can read the file by reading that particular number of bytes ahead in the normal manner. Similarly, if it is not the file you want, you can use fseek() to move forward the number of bytes in the file you are skipping and be ready to read in the header info for the next file and repeat the process.

Resources