hi everyone,
i have to search a one word in txt file using C program,..I tried but output not comming right...please give me a flow for that...
i am newbie to C...
regards....
srini
13 16102
hi everyone,
i have to search a one word in txt file using C program,..I tried but output not comming right...please give me a flow for that...
i am newbie to C...
regards....
srini
Let's see the code you've written.
Let's see the code you've written.
i have read all datas into one buffer....from that I have to search the word...
see my code.... - #include <stdio.h>
-
#include <stdlib.h>
-
#include<string.h>
-
-
int main () {
-
FILE * pFile;
-
long lSize;
-
char * buffer;
-
size_t result;
-
-
pFile = fopen ( "/home/srinivas/Desktop/cl.txt" , "r+" );
-
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
-
-
// obtain file size:
-
fseek (pFile , 0 , SEEK_SET);
-
lSize = ftell (pFile); printf(" The file size is = %ld",lSize);
-
rewind (pFile);
-
-
// allocate memory to contain the whole file:
-
buffer = (char*) malloc (sizeof(char)*lSize);
-
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
-
-
// copy the file into the buffer:
-
result = fread (buffer,1,lSize,pFile);
-
-
puts(buffer);
-
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
-
-
fclose (pFile);
-
free (buffer);
-
return 0;
-
}
Hi Srini,
From the code I see that ftell(pFile) will point to the beginning of the file and hence its ouput is 0. This is because you have used SEEK_SET in the fseek func which will bring you to the begining of the file. Try changing SEEK_SET to SEEK_END in the fseek call in the code which moves the cursor to the end of your file. now ftell(pfile) will give you the cursor value which will print the output.
Hope the following code works for you. - #include <stdio.h>
-
#include <stdlib.h>
-
#include<string.h>
-
-
int main () {
-
FILE * pFile;
-
long lSize;
-
char * buffer;
-
size_t result;
-
-
pFile = fopen ( "/mydir2/narasip/zippy.c" , "r+" );
-
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
-
-
fseek (pFile , 0 , SEEK_END);
-
lSize = ftell (pFile); printf(" The file size is = %ld\n",lSize);
-
rewind (pFile);
-
-
buffer = (char*) malloc (sizeof(char)*lSize);
-
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
-
-
result = fread (buffer,1,lSize,pFile);
-
-
puts(buffer);
-
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
-
fclose (pFile);
-
free (buffer);
-
return 0;
-
}
yeah,
i know that ,but,this is not my question...i have to search one word from this text file..
Thanking you,
srinivas
Hi Srinivasan,
What ever the code u have written that works fine for the small input files. Getting entire file content into a char buffer is not good for larger files.
So, better read word by word and search it.
1) Open file.
2) loop until EOF
Read word
If the word is matched
break
loop end
hi...
i had coded like below.... - #include <stdlib.h>
-
#include<string.h>
-
-
int main (void) {
-
-
FILE *Input;
-
-
long lSize;
-
-
Input = fopen("/home/srinivas/Desktop/cl.txt","r+");
-
-
if (Input ==NULL) {fputs ("File error",stderr); exit (1);}
-
-
fseek (Input ,0 , SEEK_END);
-
-
lSize = ftell (Input); printf(" The file size is = %ld",lSize);
-
-
char string1[24];
-
-
-
printf("\n Enter the string you want to search \n");
-
-
gets(string1);
-
-
printf("\n The read string is =%s ",string1 );
-
-
//do{
-
-
int i=0;
-
char string2[24];
-
-
-
-
/* Read one word for the text file */
-
-
do{
-
while( fgets(string2,20,Input) != '\0')
-
{
-
//string2[i] = fgetc(Input);
-
//printf("%c",string2);
-
//i++;
-
}
-
-
printf("the string2 is %s =",string2);
-
printf(" In do loop");
-
-
-
if( strcmp(string1,string2) == 0)
-
{
-
printf(" string successfully searched : string2 = %s",string1);
-
break;
-
}
-
-
else
-
{ printf(" string Not found");
-
}
-
}while(!feof(Input));
-
-
-
}
I got output Like below....
The file size is = 647579
Enter the string you want to search
SELVA
The read string is =SELVA the string2 is \uffff\u04bf\uffff\uffff\uffff\uffff,\uffff\uffff\ uffff\u04bf\uffff\uffffSELVA = In do loop string Not foundsuse:
wheather my code is correct???
for searching a word in a file....
This is ur code partly modified.... - rewind(Input);
-
/* Read one word for the text file */
-
-
while(!feof(Input))
-
{
-
fscanf(Input, "%s", string2);
-
-
printf("The string2 is: %s\n",string2);
-
-
if ( strcmp(string1,string2) == 0)
-
{
-
printf("String successfully searched\n");
-
break;
-
}
-
}
-
if (feof(Input))
-
{
-
printf("string search failed\n");
-
}
finally close the file.
vijay
This is ur code partly modified....
rewind(Input);
/* Read one word for the text file */
while(!feof(Input))
{
fscanf(Input, "%s", string2);
printf("The string2 is: %s\n",string2);
if ( strcmp(string1,string2) == 0)
{
printf("String successfully searched\n");
break;
}
}
if (feof(Input))
{
printf("string search failed\n");
}
finally close the file.
vijay
sorry vijay,
This file not working ,it was printing all names in the file...
i want to search One seperate word like "ABC" in text file...
Thanks,
srini...
no..no.. it's working. I have tested this.
can u post ur code again... i will see...
sorry vijay,
This file not working ,it was printing all names in the file...
i want to search One seperate word like "ABC" in text file...
Thanks,
srini...
no..no.. it's working. I have tested this.
can u post ur code again... i will see...
I had tried one more method ...it is working successfully...
see the code.... - #include <stdio.h>
-
#include <stdlib.h>
-
#include<string.h>
-
-
int main (void) {
-
-
FILE *Input;
-
-
long lSize;
-
-
size_t result;
-
-
char *buffer;
-
-
Input = fopen("/home/srinivas/Desktop/cl.txt","r+");
-
printf("Input value: %d\n",Input);
-
-
if (Input ==NULL) {fputs ("File error",stderr); exit (1);}
-
-
fseek (Input ,0 , SEEK_END);
-
-
lSize = ftell (Input); printf(" The file size is = %ld\n",lSize);
-
-
rewind (Input);
-
-
// allocate memory to contain the whole file:
-
buffer = (char*) malloc (sizeof(char)*lSize);
-
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
-
-
// copy the file into the buffer:
-
result = fread (buffer,1,lSize,Input);
-
-
long i,j,len,z;
-
-
char string1[20];
-
-
printf(" Enter the Name you want to search \n");
-
-
gets(string1);
-
-
len = strlen(string1);
-
-
//printf("The length of the string = %d ",len);
-
-
char string2[20];
-
-
// char *compare;
-
-
fseek (Input ,0 , SEEK_SET);
-
-
-
for(i=0;i <lSize ; i++)
-
{
-
//printf("%c ,%c \n ",string1[0],buffer[i]);
-
if(string1[0] == buffer[i]){
-
// printf("Inside if Loop\n");
-
for(z=0, j=i ;j < i+len ;j++)
-
{
-
-
// printf("Inside for Loop\n");
-
string2[z] = buffer[j];
-
z++;
-
}
-
-
// printf("The length of the string2= %d,%s ",strlen(string2),string2);
-
-
if(strcmp(string1,string2) == 0)
-
{
-
printf("string successfully searched\n ");
-
}
-
-
//else
-
// printf("string Not found\n");
-
}
-
}
-
-
-
-
}
hi...
I have text file.one part of a file ..i given below..
7. LPA.SR 36917/2006 M/S.V.SELVARAJ
D. JAYASINGH
V.S.MANIMEKALAI
and
MP.SR 67318/2006
FOR EXTENSION OF TIME
~~~~~~~~~~~~~~~~~~~~~
8. OSA.323/2006 M/S.AIYAR AND DOLIA M/S.RAM AND RAJAN & ASSOCTS.
V. KALYANARAMAN FOR R1
R2 OFFICIAL LIQUIDTOR
R3 TIMES GURANTEE LTD MUMBAI - 1
and
OSA.324/2006
MP.1/2006
MP.2/2006
FOR DIRECTION
~~~~~~~~~~~~~
9. (NOT READY IN NOTICE)
WA.1246/1998 M/S K.BALASUBRAMANIAN MR.M.V.VENKATASESHAN FOR R1
and
CMP.13730/1998
WA.1247/1998 MR.A.FATHIMANATHAN MR.M.V.VENKATASESHAN FOR R3
and
CMP.13731/1998
FOR JUDGMENT
~~~~~~~~~~~~
Now what my next plan is ...I have to search one string..that was done already...
I have to put tags for eg..$$ into before starting of serial No 7..and also put tag into befor serial no 8...
------------------------------------------------------------------------------------------------------------
for eg....
$$ 7. LPA.SR 36917/2006 M/S.V.SELVARAJ
D. JAYASINGH
V.S.MANIMEKALAI
and
MP.SR 67318/2006
FOR EXTENSION OF TIME
~~~~~~~~~~~~~~~~~~~~~ $$
8. OSA.323/2006 M/S.AIYAR AND DOLIA M/S.RAM
-------------------------------------------------------------------------------------------------------------
This is my requirement...
give me suggestions
try: strstr - while(!file.eof())
-
{
-
setline(file,&line);
-
strstr(line, "ABC")
-
}
look up strstr command
otherwise you can do a simple way but not good. but i am sure it is working. - char SearchName[4]= "ABV";
-
int time;
-
for (time=0, time< 1000, time++) // time value depends the size of the file
-
{
-
scanf ("%c", &buffer[time]);
-
while(SearchName[0]==buffer[time])
-
-
{
-
time++;
-
while(SearchName[1]==buffer[time])
-
-
{
-
time++;
-
while(SearchName[2]==buffer[time])
-
{
-
//search successful.
-
}
-
-
}
-
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Julie |
last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB)
for a given string.
The files are unindexed and unsorted, and for the purposes of my immediate
requirements, can't...
|
by: tshad |
last post by:
Can you do a search for more that one string in another string?
Something like:
someString.IndexOf("something1","something2","something3",0)
or would you have to do something like:
if...
|
by: Eric |
last post by:
Hi: I have two files. I search pattern ":" from emails text file and
save email contents into a database. Another search pattern " field is blank. Please try again.",
vbExclamation + vbOKOnly...
|
by: Dameon |
last post by:
Hi All,
I have a process where I'd like to search the contents of a file(in a dir)
for all occurences (or the count of) of a given string. My goal is to focus
more on performance, as some of the...
|
by: nganglove |
last post by:
C++ string search
--------------------------------------------------------------------------------
Hello, please can any one help me? I am given an assigment in C++ to read a text file and...
|
by: princymg |
last post by:
I want to search a file from server and want to copy it to the local disk.
how it is done? This is working if the file is in my hard disk itself.But not when it comes to server. If i map the server...
|
by: =?Utf-8?B?UGVycmlud29sZg==?= |
last post by:
Not sure where to post this...
Found some interesting behavior in Windows Search (Start =Search =All
files and folders =search for "A word or phrase in the file:"). This
applies to XP and maybe...
|
by: slizorn |
last post by:
hi guys,
i need to make a tree traversal algorithm that would help me search the tree..
creating a method to search a tree to find the position of node and to return its pointer value
basically i...
|
by: JamesOo |
last post by:
I have the code below, but I need to make it searchable in query table,
below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
|
by: Debadatta Mishra |
last post by:
Introduction
In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
| |