You may want to try fgets/sscanf. Maybe something like this.
#include <stdio.h>
int main(void)
{
const char filename[] = "file.txt";
FILE *file = fopen(filename, "r");
if ( file )
{
char line [ BUFSIZ ];
while ( fgets(line, sizeof line, file) )
{
char substr[32], *ptr = line;
int n;
fputs(line, stdout);
while ( *ptr )
{
if ( sscanf(ptr, "%31[^:]%n", substr, &n) == 1 )
{
ptr += n;
puts(substr);
}
else
{
puts("---empty field---");
}
++ptr;
}
}
}
else
{
perror(filename);
}
return 0;
}
Then, if this is too slow, I'd look into other faster techniques.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
My highlighing in red: Rob Pike , a leading expert on applying the C programming language , offers the following "rules" in Notes on Programming in C as programming maxims (but they can be easily viewed as points of a Unix philosophy):Rule 1. You can't tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you've proven that's where the bottleneck is.
Rule 2. Measure. Don't tune for speed until you've measured, and even then don't unless one part of the code overwhelms the rest.
Rule 3. Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don't get fancy. (Even if n does get big, use Rule 2 first.)
Rule 4. Fancy algorithms are buggier than simple ones, and they're much harder to implement. Use simple algorithms as well as simple data structures .
Rule 5. Data dominates. If you've chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.
Rule 6. There is no Rule 6.
Pike's rules 1 and 2 restate Donald Knuth 's famous maxim, "Premature optimization is the root of all evil." Ken Thompson rephrased Pike's rule 4 as "When in doubt, use brute force." Rule 5 was previously stated by Fred Brooks in The Mythical Man-Month .http://en.wikipedia.org/wiki/Unix_philosophy
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Do you something mean like this?
#include <stdio.h>
int main(void)
{
const char filename[] = "file.txt";
FILE *file = fopen(filename, "r");
if ( file )
{
char line [ BUFSIZ ];
while ( fgets(line, sizeof line, file) )
{
char name[16], ht[8], output[128];
int a,b,c,d;
if ( sscanf(line, "%15[^:]:%7[^:]:%d:%d::%*d:%*d:%*d:%*d:%d:%d",
name, ht, &a, &b, &c, &d) == 6 )
{
snprintf(output, sizeof output,
"CUST:NAME=%s,HT=%s,&%d&%d,LT=PT-%d&PQ-%d",
name, ht, a, b, c, d);
puts(output);
}
}
}
else
{
perror(filename);
}
return 0;
}
/* my output
CUST:NAME=9890000501,HT=74667,&0&6,LT=PT-0&PQ-5
*/
Sure -- but the fields in each record would need to be all filled or all empty in the same places. And the format string can get a bit unwieldy -- unless you are only looking for the first several fields out of it.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
I guess sscanf wont work since i have fields with variable length delimited by ":"
%31[^:]
will it expect minimum 31 chars..i dont understand it :(
No, it would prevent overflowing a 32-char buffer by writing a maximum of 31 characters plus the null.Can i use fprintf() to write straight to the file??Yes.Since it is variable length fields i will have to clear the buffer for each record and memset would be expensive!!Why? Overwriting strings make clearing the buffer(s) irrelevant.I believe it wont write "\0" after each string ..right?Wrong.Using fprintf(), can i use "\n" for the succeding fields to be written to the next line?Yes.I want the lines in my file to be separated by a carriage return and a line feed.
Is it the same as "\n"?????It is if that is how your system translates a newline in text mode.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
If i use fprintf() and i use multiple "%s" arguments to create the string(buffer) to write to the file,
Will the file have a "\0" after each string or just the contents of the string will be present??
Just the contents of the string will be present.The program is to run on a SUN netra server.
I dont know how it interprets a "\n"In Unix, a newline is just the'\n'.
The ASCII text file needs to have each line separated by a carriage return and a line feed..This strikes me as somewhat odd since you mention Unix.While opening a file using vi or other Unix editor how can we make sure that the END of line has a carriage return and a line feed???Use"\r\n" if that is what you need.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Actually the file generated on solaris is to be sent to a PC.
I tried a "\015\n" and it works fine :)
Thanks!!!
Why not use'\r' instead of '\015'? While they may both resolve into the same character for ASCII, the '\r' version is also portable to other systems. And I would think it is easier to understand the meaning of '\r' as opposed to '\015'.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
No, I don't really know of any performance issues.
If it runs too slow, profile the code to find bottlenecks. If the file I/O is too slow, you'd likely need to used something platform-specific instead of the standard functions anyways.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>Earlier it was mentioned "%31[^:]%n"
Can somebody please explain this format???
>Whats [^:]
The %31[^:] specifier means to read up to 31 non-colon characters into a string.
[edit]Check this out, too[/edit]
>whats %n..shouldnt i use %s???
No. The %[ specifier works like a string. The %n is a completely different specifier that fills an integer with the number of characters actually read (into the string).
>If possible,also please point to a online tut or man that explains the format specifiers .
Here is a man page.
>I have a file with lines as below
>fname:lname:1::2:3:1234:123
Try something like this .
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>How do i remove the initial space from the first string.
If you know that it will always be there, you can simply begin the format string with %*c to read and discard it.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314