Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8704267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:03:31+00:00 2026-06-13T03:03:31+00:00

I want to simplify a txt document and I tried this code: #include <iostream>

  • 0

I want to simplify a txt document and I tried this code:

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    // 1. Step: Open files
    FILE *infile;
    FILE *outfile;
    char line[256];
    infile = fopen("vcard.txt", "r");
    outfile = fopen("records.txt", "w+");
    if(infile == NULL || outfile == NULL){
         cerr << "Unable to open files" << endl;
         exit(EXIT_FAILURE);
    }

    // 2.Step: Read from the infile and write to the outfile if the line is necessary
    /* Description:
    if the line is "BEGIN:VCARD" or "VERSION:2.1" or "END:VCARD" don't write it in the outfile
    */

    char word1[256] = "BEGIN:VCARD";
    char word2[256] = "VERSION:2.1";
    char word3[256] = "END:VCARD";

    while(!feof(infile)){
        fgets(line, 256, infile);
        if(strcmp(line,word1)!=0 && strcmp(line,word2)!=0 && strcmp(line,word3)!=0){ // If the line is not equal to these three words
          fprintf(outfile, "%s", line); // write that line to the file
        }
    }

    // 3.Step: Close Files
    fclose(infile);
    fclose(outfile);

    getch();
    return 0;
}

Unfortunately, despite the infile includes word1, word2 and word3 hundred times I still get 1 or -1 as the return value of strcmp.

What should I try?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:03:32+00:00Added an answer on June 13, 2026 at 3:03 am

    fgets returns the newline character as part of the string. Since the strings you are comparing against don’t contain a newline, they will be compared as different.

    Since you are writing in C++, you may want to use std::ifstream and std::getline to read the file. The strings returned by getline will not have the newline in them, and as an added bonus you won’t have to specify a limit on the line size.

    Another (unrelated) issue: Using while (!foef(file)) is wrong, and can result in the last line being read twice. Instead, you should loop until fgets returns a null pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to simplify the following code: public static void main(String[] args) { int
I have this code snippet that I want to simplify: switch (typeString) { case
I want to simplify this expression a bit. Is there a better way to
I want to create two sites on RubyOnRails and would like to simplify this
I have many callbacks and I want to simplify or clean up the code.
want to know why String behaves like value type while using ==. String s1
Want to code a key pad for an calculator. What I want to make
I know that to create a file in c++ we use following code #include
I just want to simplify what I am doing before, having multiple php files
In Perl, I can do this: push(@{$h->[x]}, y); Can I simplify the following python

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.