Solved

finding first word

Posted on 2004-09-01
21
202 Views
Last Modified: 2010-04-15
how to find the first word of a line from a file...........the line number will be given by the user or generated by the program
0
Comment
Question by:nand_kishore
  • 7
  • 5
  • 3
  • +2
21 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11960244
Are your lines fixed length of variable length?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11960289
If your lines are variable length,you'd have to do:

#define MAX 1000
//MAX should be a large number greater than the max. line length expected
FILE *f;
f=fopen("textfile.txt","r");
int lines=5;//if you want to read the 5th line from file
char buffer[MAX];
for(i=0;i<lines-1;i++)
fgets(buffer,MAX,f);
//Now you are at the start of the line you want,read the first word using either fgets,fscanf etc.

fclose(f);
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11960322
If the lines are fixed length,you can jump to the reqd line in one go,using fseek()
fseek() from the start of the file by (lines-1)*(line_length+1) to reach the start of the reqd line.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11960529
>how to find the first word of a line from a file
>the line number will be given by the user or generated by the program

The basic idea in either case is same. To reach the specified line and read the first word.

As with most files, I would expect your file to have variable length lines. I would add a few things and make some corrections to the code already provided by ankur just to make it more robust and complete.


int main ()
{
        int num,i;
        FILE * fp;
        char buffer[MAX_LEN], word[MAX_LEN]={ 0 };

        printf (" Enter the line number: ");
        scanf ("%d",&num);
        if ( num <= 0 )
        {
                  printf ("invalid line number\n");
                  return 1;
         }

        fp = fopen ("my_file", "r" );
        if ( fp == NULL )
        {
                  printf ("could not open my_file\n");
                  return 1;
        }
        i=0;
        while ( i<num && fgets (buffer, MAX_LEN, fp ) != NULL )
                 i++;
        if ( !feof(fp) )
        {
              sscanf(buffer,"%[^ ]",word);
               printf (" first word of %d line is %s\n", num, word );
               fclose (fp);
               return 0;
        }
        else
        {
               printf ("not enough lines in the file\n");
               fclose(fp);
               return 1;
         }
}
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11960545
Ofcourse you need to add
#define MAX_LEN 256

as the first line of code.
0
 
LVL 2

Accepted Solution

by:
sneeuw_chan earned 250 total points
ID: 11962315
I think it'd be a lot easier if you use fscanf to its fullest, like so: (untested)

char word[256];

for (i = 0; i < num; i++) {
    if (fscanf(FILE, "%*[^\n]\n") == EOF) {
        perror("Line number too large");
        exit(1);
    }
}

if (!fscanf(FILE, "%256s", word)) {
    perror("Error scanning word");
    exit(1);
}

printf("The word is: '%s'\n", word);
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 11975078
@sneeuw
no regex plz. C doesn't have builtin regex lib. do this in java or perl. Excuse me if that sounded rude. :)
0
 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 11975366
That was genuine C code.  The bit that looks like a regex is actually a sscanf feature.
If you use the '[' character in an sscanf, you scan for a string that includes the characters between [ and ] (or excludes if it starts with ^)  Sure, that looks like a regex, but it is a standard sscanf feature.  (When I say standard I mean 'ANSI C' standard.)  Reat the manpage for sscanf.  Sorry if that sounded rude and a bit repetetetive. :)
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 11976012
I aplogize.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 11976096
No prob.  It's a very obscure feature, but very useful.  IMO one of the most useful features of scanf even.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11979916
Hi sneeuw_chan,

That is another way of doing it and it quite similar to what i and sunny described,just that we used fgets where you used fscanf.

Also,this invokes undefined behaviour:
>fscanf(FILE, "%*[^\n]\n")

You specify a format parameter but dont provide the variable to store the value in.
0
 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 11979956
The reason I used fscanf is precisely because I did *not* provide a variable to store the value in.
The '*' in the format string suppresses assignment of that parameter; it is simply discarded.
That means you don't have to care about allocating buffers for a maximum line length, just for a maximum word length.
Otherwise I wouldn't have bothered supplying this alternative.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11980385
Yeah.You're right.

Seriously,i didnt know about the * flag in *scanf.Thanx.It does make *scanf more useful.
0
 

Expert Comment

by:lebuihung
ID: 11983554
function to read from a specified file and line number

#include <string.h>
#define LINE_LEN 80


char* readLine(char* filenName, int lineNo)
{
   FILE* fptr;
   char  buffer[LINE+1];
   char* firstWord;


   fptr=fopen(fileName,"r");
   if(fptr==NULL) /*open file error */
      return NULL;
   
   firstWord=malloc(sizeof(char)*(LINE_LEN+1)); /* memory allcation */

   
   while(fgets(buffer,LINE_LEN,fptr)!=NULL&(lineNo--)>=0); /* keep reading until line lineNo th */

   if(buffer==NULL)
      return NULL;   /* number of line in file is smaller than lineNo-> error */

   /* else using string token to read first word */
   strcpy(firstWord,strtok(buffer," "));

   
   fclose(fptr); /* close file */
   free(buffer); /* free memory location */

   return firstWord;
}

Hope it will help.
0
 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 11985294
Yuk.  A hardcoded line length, and you're not even checking if fgets reads partial lines..  What is it with C coders and hardcoded buffer sizes anyway...
0
 

Expert Comment

by:lebuihung
ID: 11986149
what's the matter with hardcoded line length ? if u want, u still can check to c whether the new line character '\n' is in the new read input -> u can know that a whole line has been read.

while(fgets(buffer,LINE_LEN,fptr)!=NULL)
{
    if(buffer[strlen(buffer)-1]=='\n')
         lineNo--;
}

by doing as above, u can check if fgets reads partial lines or whole line. This is because fgets included '\n' new line char as a part of read in.

0
 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 11986229
I know quite well how fgets works, but I find it disturbing that I need to point out to you that you weren't checking for partial lines.  The way you're checking is pretty inefficient too, by the way.  You do know that strlen() is O(N), right ?
Hardcoded line lengths are a cause of a lot of serious security leaks.  And in this case, it's so easy to overcome that it's really not excusable.
Even without the use of fscanf, you could cimply use fgetc and count newlines, that way you wouldn't even need a buffer.
But that functionality is already present in libc; you can simply use fscanf(fptr, "%*[^\n]\n") to scan for the next newline.
0
 

Expert Comment

by:lebuihung
ID: 11986248
thanks for ur help :-). I know hardcoded line lengths only would caused a lot of security problems. But by using hardcoded line lengths and fgets, i think u can prevent that :-) anyway, i think i should stop here before we screw up this topic. Cheers.
0
 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 11987670
Agreed.  Sorry I came over so harsh back there.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now