-1

Program:

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
using namespace std;

int main()
{
    FILE *f;
    char* line;
    size_t ln=100;
    char* s;
    line=new char[100];
    s=new char[100];
    cout<<"input key"<<endl;
    cin>>s;
    f=fopen("parvin.txt","r");
    if(f==NULL)
    {
        cout<<" no file TO read so creating for writing "<<endl;
        //return 0;
        f=fopen("parvin.txt","w");
        fputs(s,f);
        fputc('\n',f);
    }
    else
    {
        while(! feof(f))
        {
            fgets(line,100,f);
            cout<<line<<endl;

            //if(!strncmp(line,s,strlen(line)-1))
            if(strcmp(line,s)== 0 )
            {
                cout<<"duplicate found"<<endl;
                fclose(f);
                return 0;
            }
        }
        fclose(f);
        f=fopen("parvin.txt","a+");
        fputs(s,f);
        fputc('\n',f);
    }
    fclose(f);
}

Here the above program where I like to read an input string and write it into file provided the string is not present already in file.

  • take input string
  • open file in read mode.

  • if it is first time entry file will not be there if file pointer return NULL, create a file to write mode and write the inputted string.

  • if file already there then read file line by line and compare with input string if match with any line then return and close.
  • other wise open the same file in write mode and write the inputted string.

But it is not working properly..

strcmp not executing properly.... with the duplicate entry also it dont go into that loop of "duplicae found" .

please if anyone can help ...

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • 4
    You should not be using C and C++ methods together like that. Choose one and stick with it... – Jeff Mercado May 11 '15 at 06:16
  • I know but in " c" printf and scanf I hate to use them. format specifier you know... just for cin and cout I use that C++ lib..in original code I will gonna use only c. – Parvin Kumar May 11 '15 at 07:55

2 Answers2

5

The fgets:

fgets(line,100,f);

consumes the newline character from f and stores it in line. But s doesn't contain the newline character. So, the strcmp returns a non-zero number as the strings(s and f) are different.

Strip the newline character by using

line[strcspn(line, "\n")] = '\0';

just after the fgets. The strcspn function, in your case, returns the number of characters until a \n in line. If \n is not found in line, it returns the length of the string line(strlen(line)).


Also, read Why is while ( !feof (file) ) always wrong?. Replace
while(!feof(f))

with

while(fgets(line,100,f)) //Same as `while(fgets(line,100,f) != NULL)`

and don't forget to remove the fgets from the body of the loop to fix this issue.

Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
-1

Use while(fgets(line,100,f)!=NULL)

Par26
  • 1
  • 5
  • That was not the problem ...problem was with the new line character in each line while comparing to inputted string. – Parvin Kumar May 11 '15 at 07:57