|
-
Apr 24th, 2002, 09:01 PM
#1
Thread Starter
New Member
using C to read and write from another file
Can anyone teach me how to use C to read a file with extension .csv then get some data then make calculation and input the calculation into another file with the same extension.
-
Apr 26th, 2002, 09:26 AM
#2
No. You can read up about file i/o in the tutorials at the top of the page or one of links from there. You know the format of a csv file, so you can read the input in once you know the i/o. Making the calculations is easy (depending on the complexity of the calculations) and writing the result to another file is even easier.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 26th, 2002, 11:16 AM
#3
PowerPoster
I think what CornedBee is telling you in his somewhat blunt but accurate advice is that you are asking a more generic question than you seem to realize. "CSV" just means that the file is ASCII and that the data fields are separated by commas. (CSV = Comma Separated Variable).
Thus reading a CSV file is no different from reading any other flat file and that's such a fundamental thing that it's a little like your asking us to push on your chest to help you breathe. There really are some fundamental things you have to learn yourself. If you study up on how to read files and find something you don't understand, you'll find tons of folks on this board that will be glad to help.
Likewise, outputing to a CSV file just means that your data needs to be formatted into a string that has the data fields separated by commas and thus it's the same as outputting to any other flat file.
Internally, dealing with a CSV file is conceptually trivial, and I'll be happy to provide you with a subroutine that will do most of the work, but you'll have to write the wrapper. In fact, it's so short, I'll just attach it here.
//************************************************************************
//
// csv_separate --- separate a comma separated variable input line
// into fields in a caller-specified char array
//
// no check is made for field length. this
// function assumes that the specified array can
// handle the biggest field encountered
//
// there is an internal limit on line size of 256 chars
//
//************************************************************************
int csv_separate( // return:
// 0 for success
// -1 for too many input fields
// -2 input line is too long
char *line, // line to separate
char **ca, // where to put the fields
int maxfields) // max number of csv fields
{
int ix; // index on input line
int iy; // index on which array line
int iz; // index on current array line
char tempc;
ix = 0;
iy = 0;
//
// loop to work on one input line
//
while(1)
{
//
// loop to separate out one field
//
iz = 0;
while(1)
{
tempc = line[ix++]; // get the next input character
if (ix >= 256) // if the input line is too long,
return -2; // do an error exit
if (tempc == EOS) // if the line is done,
break; // break the inner loop
else if (tempc == ',') // if this field is done,
break; // break the inner loop
else
{
ca[iy][iz] = tempc;
iz++;
}
} // end while getting one field
ca[iy][iz] = EOS; // terminate the array line
if (tempc == EOS) // if the line is
break; // done, quit
else // otherwise, just increment the
iy++; // index on which array line
if (iy > maxfields) // if there are too many fields,
return -1; // do an error exit
} // end while doing one input line
return 0; // flag success
} // end function csv_separate
note that this code assumes the existance of an array (CA) in the calling space that is big enough to hold all the fields, and of course you'll have to call this function once per line from the input file
-
Apr 26th, 2002, 11:17 AM
#4
PowerPoster
sorry about how awful the code looks. I haven't pasted code here before and didn't realize that it removes all spaces, so my comments make it more messy, not more understandable.
-
Apr 26th, 2002, 02:17 PM
#5
The forum supports some formatting -
put
[ c o d e ] [ / c o d e ]
around your code fragment (minus the spaces)
you can also use [ p h p [ / p h p ]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|