Matching first n characters in a line in a file


 
Thread Tools Search this Thread
Top Forums Programming Matching first n characters in a line in a file
# 1  
Old 01-07-2013
Matching first n characters in a line in a file

hi to all,

i am writing a simple file program in C which has to take a string of length n as input and match that input string with first n character of the line in file. if it does not match it has to compare with first n characters of next line. i am finding it difficult to know how exactly to use fseek function to achieve this. please help...

thank you.
# 2  
Old 01-07-2013
I see no point using fseek here. fseek has nothing to do with a file's contents. You will have to read lines one by one, which is what fgets() is for.


Code:
char buf[4096];
FILE *fp=fopen("filename", "r");

while(fgets(buf, 4096, fp) != NULL)
{
        printf("Read line %s\n", buf);
}

fclose(fp);

As for comparing two strings of known lengths, that's what strncmp is for.

Code:
char *string1="abcdefg";
char *string2="abcdqwerty";

if(strncmp(string1, string2, 4) == 0)
{
        printf("The first four characters of these strings are the same\n");
}

# 3  
Old 01-07-2013
thank you.

u made it so simple... thank you vary much..Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Using awk to add length of matching characters between field in file

The awk below produces the current output, which will add +1 to $3. However, I am trying to add the length of the matching characters between $5 and $6 to $3. I have tried using sub as a variable to store the length but am not able to do so correctly. I added comments to each line and the... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

4. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

HI Can any one guide me how to achieve this task. I have 2 files env.txt #Configuration.Properties values identity_server_url = http://identity.test-hit.com:9783/identity/service/user/register randon_password_length = 6 attachment_file_path = /pass/temp/attachments/... (1 Reply)
Discussion started by: nikilbr86
1 Replies

5. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

6. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

7. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

Hi I am not the best scripter in the world and have run into a issue which you might be able to guide me on... I have two files. File1 : A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB File2: C345,... (5 Replies)
Discussion started by: luckycharm
5 Replies

8. Shell Programming and Scripting

Input file line matching

Hi there, I have two input files. I need to match the last two columns of each line in File1 with the first two columns of a line in File2. Example: File1 AAA BB 234 789 BBB CC 426 624 CCC DD 356 643 File2 766 332 12 234 789 64 122 633 23 426 624 88 777 453 22 356 643 92 ... (3 Replies)
Discussion started by: palex
3 Replies

9. Shell Programming and Scripting

using perl for matching one file with another file and print into new line

One file is fileA 0.0246*0.0068*0.0013*0.0023*0.0182*0.0028*0.0019*0.4750*0.0028*0.0812*0.0123*0.0018*0.0039*0.0020*0.0028*0.0047*0.0139*0.3330*0.0017*0.0072*0.4789... (4 Replies)
Discussion started by: cdfd123
4 Replies

10. Shell Programming and Scripting

how to delete a matching line from a file

I have file A with 10 lines and file B with 15 lines I have to read each line from file A and match it with lines in file B If matching lines are found i need to delete it from file B The ouput will be the deleted file B I tried the following in the loop but the deleting part is not... (5 Replies)
Discussion started by: codeman007
5 Replies
Login or Register to Ask a Question