Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make scanf() read first word of a line then jump to next line

Status
Not open for further replies.

felix345

Technical User
Feb 1, 2002
1
US
I am right writing a post processor for the Grep command, since the grep outputs the number of the lines that there was a hit, So I want to read the number(ie the first word of the line then jump to the next line to read the first word again.
thank for any help given
 
This is totally OT and I hope I don't get lambasted for it.

What you are trying to do is much harder in C than just something like this:

#!/bin/sh
lines=`grep -n "me" $1 | sed 's/:.*//g'`

for all in $lines
do
awk -v line=$all ' {
if (NR == line) {
for (i = 0 ; i <= NF ; i++) {
if (i == 1) {
print $i
}
}
}
}' $1
done

But I am interested in seeing the equivalent program
in pure C :)
 
Okay, lets presume your file is text and that each line of text is terminated by a new line character.

open file to read text,
fp = fopen(filename, &quot;rt&quot;);
then loop through each line of text and increment the counter like so

int counter = 0;
char str[line_length]; // string of required length
FILE *fp;

fp = fopen(filename, &quot;rt&quot;);
while(fscanf(fp,&quot;%[^\n]\n&quot;, str)!=EOF)
counter++;
printf(&quot;File holds %d lines of text&quot;, counter);
// this part of the code - &quot;%[^\n]\n&quot;- will read a string of characters up to the new line character - the extra \n then discards the new line character

OKAY
if file is binary file and you know the structure of each record.

struct myrecord {
// your structure members
};

int main (void)
{
FILE *fp;
int counter;

fp = fopen(filename, &quot;rb&quot;);
fseek(fp, 0L, SEEK_END);
counter = ftell(fp)/ sizeof(struct myrecord);
printf(&quot;File holds %d lines of text&quot;, counter);
// remember to reset file pointer to start of file
// before carrying out further operations on the file
fseek(fp, 0L, SEEK_SET);
// remainder of your code for whatever you require
} //end main

Hoping to get certified..in C programming.
 
Humm,
Well, I think there can be a better solution using just scanf with its powerful format specifier stirng. See this:

Code:
scanf(&quot;%s %[A-Za-z0-9.- ]&quot;,str_array);

This line of code is designed to read the string (word) before the first space and later on it will read all character in a void string that can contain A-Z, a-z 0-9 . (dot) and - (hyphen) characters till the end of the line.

Loop this statement and it will get you the first word from every line.
Code:
%[A-Za-z0-9.- ]
does the magic! its a specific format specifier for strings... put in the chars you want the scanf statement read for you...

I hope I got you a precise solution :p

have fun coding...;-)

Roy.
user.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top