what I want to do is I have two txt files named as number.txt and numAndName.txt
In number file it contains only one character and it is a number.
IN numAndName file it contains both number and Names as follows
5 name5
4 name4
3 name3
2 name2
1 name1

then what I want is to open both files and get the name from numAndName file corresponding to the value in the number file. If number file contains 3, out put must be name3

I have up to fopen();

but I cant split the text from white spaces and also Its very confusing that how to go to the next line.
Please help...

OK. First, open the number file, then open the number and name file. Here, use fgets(), to put the whole row at a time, into a char buffer array.

Include string.h to allow:
len=strlen(buff), after you've fgets() a row of text from the name and number file.

Then your buffer will look like this:

name4\n\0, with the last two char's not being visible, but they'll be there. So we're going to trim them off:

buff[len]-2 will be the number you want. We're repeat this for every row of text in the name and number file, until either we find the right number, or the file runs out of data for us.

If you'll post up a bit of code, and tell us where you're stumped with this, we can be more helpful.

Why do you want to open the num file when both the number and name are present in the numAndName file ?

I use number file to store a result, from a separate program.(This value get replaced by that program)
And all the data are included in NumAndName File.
So what I want is to compare and get the Name who owns that number which contain in Number file


Post back if you get lost in my description, Itsmeisuru. Sometimes when I get into describing a solution, my writing ability goes sideways and hits the guard rails. ;)

Hi, this is my testing code.

#include <string.h>

int main() {
	char buff[256]; 
	FILE *f1;
	FILE *f2;
	f1 = fopen("1.txt", "r");
	f2 = fopen("22.txt","r");
	len = strlen(buff);
	return 0;
	//char *fgets (char *str, int size, FILE* file);

I write this code to test, but unfortunately it gives some other errors FILE undeclared.

something I should emphasize is:

if the nameAndNumber file as follows

3 Michel
2 Anne
1 Declan

so if num file gives 2 my program must out put 'Anne'
so please help me, im lost
And early code what you have written "name4\n\0" was not clear to me.
I googled but couldnt get a proper answer.

Hi sorry, this is my new code. I could develop that to this extend. What I want is to compare as I told: I think it should be inside the while loop(I have commented in my codes below)
Please someone help me.

#include <string.h>
#include <stdio.h>

int main() {
	char buff1[256]; 
	char buff2[256];
	FILE *f1;
	FILE *f2;
	int len;
	f1 = fopen("1.txt", "r");
	f2 = fopen("22.txt","r");
//	fgets()
       // if()
	len = strlen(buff2);
  // printf(len);
	return 0;
	//char *fgets (char *str, int size, FILE* file);


These aren't things you can google really. That's why I wrote "post back if you get lost".

In your first post you mentioned a "number.txt" file. Now you're indicating a 1.txt and a 22.txt file. Which leaves me confused on that point.

Is 1.txt the number file now, and 22.txt is the name and number file, maybe?

Can you make a 5 line number file, and then do the same with the name and number file, right down to the file names each one will have. Because all the details here, are what is lacking for a good solution. Do you want to open every file that has a number in the number file?

And then what should the program do once the right file is opened?

It's not hard to do this, but each detail has to be in place.

sorry, yes you are correct. 1 = number.txt and 22 = numAndName.txt
1.txt always contains only one numerical character it can be a number from 1-20.
so according to that number I must find the corresponding name from 22.txt file. To do dat i hv to read the 22.txt line by line and check the first part (after splitting from the space)
I hope now the problem is some what clearer. Sorry my English is not good.
please help if can plz gv me a sample code for reading line by line and splitting

This is your program, with a few twists to show how I'd do it, with the new format you have mentioned.

It's inefficient because the data files are not in sorted order. For small amounts of data, it's OK, however.

#include <string.h>
#include <stdio.h>

int main() {
  char buff1[4]; 
  char buff2[256];
  char name[30];
  FILE *f1;
  FILE *f2;
  int num1, num2;
  f1 = fopen("1.txt", "rt");
  f2 = fopen("22.txt","rt");

  while((fgets(buff1, sizeof(buff1),f1)) !=NULL) {
    sscanf(buff1, "%d", &num1);
    while((fgets(buff2, sizeof(buff2), f2))!=NULL){
      sscanf(buff2, "%d %s", &num2, name);    
      if(num1==num2) {
        printf("\nFound a matching number %d. Name is %s", num1, name );
    rewind(f2);      //reset f2 to the start of the file
    buff1[0]='\0';   //reset all three buffers
  return 0;

Note that the fopen() lines of code, should be followed by a test to see if either pointer is NULL. If so, the program should terminate quickly with a return 1 statement.

Whenever your program opens files, test that they have indeed been opened.

Thankyou, its working
I understand sm far..
If you could explain

fgets(buff1, sizeof(buff1),f1

sscanf(buff1, "%d", &num1);

or give me some link to refer, it would be great.
Thank you for the help...

fgets() gets a string (a row of text), at a time, from a stream (a file in this case, but it could be from the keyboard using the stream "stdin"). Input stops when either the size of input you specify is exceeded, or there is a newline char found, indicating the end of a line, in the stream's content.

fgets() will add an end of string char: '\0' to the end of the string. Also, it will put the newline char: '\n' next to the end of the string, like this:

my input string data\n\0

The best thing about it is that it will take an entire line of text at one time with no problems about spaces, punctuation, etc. and it never will overfill the buffer you have set up for it. (If pinched for room, it will leave out the end of string char, but avoid that happening, you want that end of string char on there.)

buff1 is the name of the buffer, but also serves as a (constant) pointer to the address of the base of the buffer., sizeof() gives the size of the data structure, and f1 is the file Pointer.

sscanf() is the cousin to regular scanf(), except you pass it the name of the string to scan, and it scans there, instead of from the keyboard. That's why it has that extra 'buff1" name in there.

You're very welcome. ;)

Thank you very much.
It is really helpful

I noticed this forum post and decided to contribute with some modifications. If you are interested, I took Adak's code and modified it to be more robust and better suited for larger input files. If you need to adapt this software for use in a commercial environment, you may prefer this code.

The code below is not tested... I just threw it together in a text editor, but the general idea is there. If there are any problems with it should be easy to fix.

#include <string.h>
#include <stdio.h>

#define NUMBER_FILE       "1.txt"
#define NUMBER_NAME_FILE  "22.txt"

typedef struct {
  int   Number;
  int   FileIndex;
  int   NumberNameArrayIndex;
} NumberStruct;

typedef struct {
  int   Number;
  char *Name;
  int   FileIndex;
  int   NumberArrayIndex;
} NumberNameStruct;

int main() {
  FILE             *Number_FileHandle;
  FILE             *NumberName_FileHandle;
  NumberStruct     *NumberStructArray;
  NumberNameStruct *NumberNameStructArray;
  char              Buffer[256];
  int               NumLines_NumberFile      = 0;
  int               NumLines_NumberNamesFile = 0;
  int               i;

  Number_FileHandle     = fopen(NUMBER_FILE,     "rt");
  NumberName_FileHandle = fopen(NUMBER_NAME_FILE,"rt");

  // Allocate memory for Number array
  for (fseek(Number_FileHandle, 0, SEEK_SET);(c=fgets(Buffer, 256, Number_FileHandle))!=EOF;NumLines_NumberFile++);
  NumberStructArray = malloc(NumLines_NumberFile * sizeof(NumberStruct));

  // Fill Number array
  for (NumLines_NumberFile = 0, fseek(Number_FileHandle, 0, SEEK_SET);(c=fgets(Buffer, 256, Number_FileHandle))!=EOF;NumLines_NumberFile++) {   
    sscanf(Buffer, "%d", &NumberStructArray[NumLines_NumberFile].Number);
    NumberStructArray[NumLines_NumberFile].FileIndex = NumLines_NumberFile;
    NumberStructArray[NumLines_NumberFile].NumberNameArrayIndex = -1;
  // Allocate memory for Number+Name array
  for (fseek(NumberName_FileHandle, 0, SEEK_SET);(c=fgets(Buffer, 256, NumberName_FileHandle))!=EOF;NumLines_NumberNamesFile++);
  NumberNameStructArray = malloc(NumLines_NumberNamesFile * sizeof(NumberNameStruct));

  // Fill Number+Name array
  for (NumLines_NumberNamesFile = 0, fseek(NumberName_FileHandle, 0, SEEK_SET);(c=fgets(Buffer, 256, NumberName_FileHandle))!=EOF;NumLines_NumberNamesFile++) {
    NumberNameStructArray[NumLines_NumberNamesFile].Name = calloc(strlen(Buffer)+1);
    sscanf(Buffer, "%d %s", &NumberNameStructArray[NumLines_NumberNamesFile].Number, NumberNameStructArray[NumLines_NumberNamesFile].Name); 
    NumberNameStructArray[NumLines_NumberNamesFile].FileIndex = NumLines_NumberNamesFile;

    // While your at it, quickly scan the number array and link the two structure arrays together
    NumberNameStructArray[NumLines_NumberNamesFile].NumberArrayIndex = -1;
    for (i = 0;i<NumLines_NumberFile;i++)
      if (NumberStructArray[i].Number == NumberNameStructArray[NumLines_NumberNamesFile].Number) {
        NumberStructArray[i].NumberNameArrayIndex = NumLines_NumberNamesFile;
        NumberNameStructArray[NumLines_NumberNamesFile].NumberArrayIndex = i;

  // Now the NumberStructArray[] and NumberNameStructArray[] arrays are filled
  // and linked together. So you can do whatever you want with them here.
  // If a link was not found between one of the two files, the corresponding
  // index value will be -1.

  // Free the memory and file handles

  for (i=0;i<NumLines_NumberNamesFile;i++)
  return 0;