Sponsored Content
Top Forums Programming C Code to read a word from file Post 302906245 by achenle on Wednesday 18th of June 2014 09:39:38 AM
Old 06-18-2014
How does your code get the word(s) to search for? Is there only one word, or are there more than one?

That could make a big difference in how you design your code.

Also, what do you know about the file before you start processing it? The more data you have, the better you can make your program perform. Because for a large file, scalability is going to be a problem.

Assuming you get the words to be searched via command line arguments, something like this could search a single line:

Code:
int searchLine( char *line, char **argv, int argc, int *counts )
{
    /* assume no matches - that means the "counts" array can be reused */
    int rc = 0;

    int ii;
    int jj;

    /* check input values to prevent SEGV */
    if ( ( NULL == line ) || ( NULL == argv ) || ( NULL == counts ) )
    {
        /* probably should emit an error here */
        return( rc );
    }

    /* zero out the counts array */
    for ( ii = 0, ii < argc, ii++ )
    {
        counts[ ii ] = 0;
    }

    /* determine how long the line is */
    int len = strlen( line );

    /* check if we match any of the words passed in starting at each
        character in the line of text */
    for ( ii = 0; ii < len; ii++ )
    {
        /* start from 1 instead of 0, assuming argv is the commaind-line
            args passed to main(), so argv[ 0 ] is the executable file name */
        for ( jj = 1; jj < argc; jj++ )
        {
            /* on a match, increment the count for that word and set return
                code to flag a match was found */
            if ( 0 == strcmp( &( line[ ii ] ), argv[ jj ] ) )
            {
                rc = 1;
                ( counts[ jj ] )++;
            }
        }
    }

    return( rc );
}

That should work to count words in a single line of text.

One thing that's important: having a maximum line length per file. Is that specified? If so, you can use "fgets()" and read one line at a time very simply:

Code:
FILE *ff;
char *result;
char line[ MAX_LINE_LENGTH ];

ff = fopen( fileName, "r" );
if ( NULL == ff )
{
    return( ERROR );
}
do
{
    result = fgets( line, sizeof( line ) / sizeof( line[ 0 ] ), ff );
    if ( NULL != result )
    {
        /* process line of text */
    }
}
while ( NULL != result );
fclose( ff );

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to read each word in a file

Hi, I want to read each word in a file. start at a particular character say '%' and read till another character say ')' (these two characters form the part of my file). then i want to delete the whole sentence(that is between '%' and ')' ) and keep the remaining file intact. Its urgent... (1 Reply)
Discussion started by: kaps_jhaver
1 Replies

2. Shell Programming and Scripting

Read each word from File1 and search each file in file2

file1: has all words to be searched. 100007 200999 299997 File2: has all file names to be searched. C:\search1.txt C:\search2.txt C:\search3.txt C:\search4.txt Outfile: should have all found lines. Logic: Read each word in file1 and search each file in the list of File2; if the... (8 Replies)
Discussion started by: clem2610
8 Replies

3. UNIX for Dummies Questions & Answers

Read last word of file name

Hi I am writing a script that needs to read all file from a directory and print only last word of all file names. My script: for file in /documents/files/ do $shortFile=$(file##.*) echo $shortFile done All my file names in /document/files/ directory are like unix_ubuntu but I need to... (1 Reply)
Discussion started by: watsup
1 Replies

4. Shell Programming and Scripting

To read data word by word from given file & storing in variables

File having data in following format : file name : file.txt -------------------- 111111;name1 222222;name2 333333;name3 I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2). i.e val1=11111 &... (2 Replies)
Discussion started by: sjoshi98
2 Replies

5. UNIX for Dummies Questions & Answers

how to read the second word of a text file

Folks, how to read the second word of the first line from a text file. Text file does not have any delimiters in the line and has words at random locations. Basically the text file is a log and i want to capture a number that is in second position. Appreciate your help Venu (1 Reply)
Discussion started by: venu
1 Replies

6. Shell Programming and Scripting

shell code to read and extract a file

how can i check for an unix data structure (1 Reply)
Discussion started by: jhuapaya
1 Replies

7. Shell Programming and Scripting

How to read nth word from delimited text file?

Hi i am new in scripting how i can get 2 elements from first line of delimited txt file in shell scripts. AA~101010~0~AB~8000~ABC0~ BB~101011~0~BC~8000~ABC~ CC~101012~0~CD~8000~ABC0~ DD~101013~0~AB~8000~ABC~ AA~101014~0~BC~8000~ABC0~ CC~101015~0~CD~8000~ABC~ can anyone plse help?... (3 Replies)
Discussion started by: sushine11
3 Replies

8. Programming

How can I read complete word from text file?

how can I know that the word in test file end at this point .... I have an Idea to search on ' ' , '\n' or '\0' but don't know what function can store the string before those characters .. help please ! (3 Replies)
Discussion started by: fwrlfo
3 Replies

9. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

10. Shell Programming and Scripting

Read a file and save every word in a variable to use

Hello there so i have a txt file containing word like "one two three four plus four five six". I want to save every word in the file into a variable, and then use that variable to generate real numbers and apply the arithmetic value on them. example: the txt files becomes 123 + 456 and... (10 Replies)
Discussion started by: azaiiez
10 Replies
WC(1)								   User Commands							     WC(1)

NAME
wc - print newline, word, and byte counts for each file SYNOPSIS
wc [OPTION]... [FILE]... wc [OPTION]... --files0-from=F DESCRIPTION
Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input. A word is a non-zero-length sequence of characters delimited by white space. The options below may be used to select which counts are printed, always in the following order: newline, word, character, byte, maximum line length. -c, --bytes print the byte counts -m, --chars print the character counts -l, --lines print the newline counts --files0-from=F read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input -L, --max-line-length print the length of the longest line -w, --words print the word counts --help display this help and exit --version output version information and exit GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report wc translation bugs to <http://translationproject.org/team/> AUTHOR
Written by Paul Rubin and David MacKenzie. COPYRIGHT
Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO
The full documentation for wc is maintained as a Texinfo manual. If the info and wc programs are properly installed at your site, the com- mand info coreutils 'wc invocation' should give you access to the complete manual. GNU coreutils 8.22 June 2014 WC(1)
All times are GMT -4. The time now is 05:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy