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

  • SEARCH
  • Home
  • 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 8503913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:53:06+00:00 2026-06-11T01:53:06+00:00

Possible Duplicate: c++ – printf on strings prints gibberish I would like to write

  • 0

Possible Duplicate:
c++ – printf on strings prints gibberish

I would like to write several strings to file . The strings are

37 1 0 0 0 0
15 1 0 0 0 0
33 1 0 0 0 0
29 1 0 0 0 0
18 1 0 0 0 0
25 1 0 0 0 0

I first would like to store each line as elements of a string array, then call the same string array and write its element to file.

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

int writeFile() {

  char line[100];
  char* fname_r = "someFile_r.txt"
  char* fname_w = "someFile_w.txt"; 
  vector<string> vec;

  FILE fp_r = fopen(fname_r, "r");
  if(fgets(line, 256,fp_r) != NULL)   {
     vec.push_back(line);
  }

  FILE fp_w = fopen(fname_w, "w");
  for(int j = 0; j< vec.size(); j++) {
    fprintf(fp_w, "%s", vec[j]); // What did I miss? I get funny symbols here. I am expecting an ASCII
  }

  fclose(fp_w);
  fclose(fp_r);
  return 0;
}
  • 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-11T01:53:08+00:00Added an answer on June 11, 2026 at 1:53 am

    The format specifier "%s" expects a C-style null terminated string, not a std::string. Change to:

    fprintf(fp_w, "%s", vec[j].c_str());
    

    As this is C++, you should consider using ofstream instead which are type-safe and accept std::string as input:

    std::ofstream out(fname_w);
    if (out.is_open())
    {
        // There are several other ways to code this loop.
        for(int j = 0; j< vec.size(); j++)
            out << vec[j];
    }
    

    Equally, use ifstream for input. The posted code has a potential buffer overrun:

    char line[100];
    ...
    if(fgets(line, 256,fp_r) != NULL)
    

    line can store a maximum of 100 characters but the fgets() is stating that it can hold 256. Using std::getline() removes this potential hazard as it populates a std::string:

    std::ifstream in(fname_r);
    std::string line;
    while (std::getline(in, line)) vec.push_back(line);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# driver development? I would like to know if I can do
Possible Duplicate: how to delete a file? My application first reads the fields in
Possible Duplicate: Difference Between Equals and == in which cases equals() works exactly like
Possible Duplicate: How would I identify if a website originates from a mobile browser?
Possible Duplicate: git - removing a file from source control (but not from the
Possible Duplicate: Trying to get tables next to each other horizontal I have two
Possible Duplicate: Parsing XML String in C# How to process the below XML file
Possible Duplicate: How to store an IP in mySQL I want to get the
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: Why do I see a double variable initialized to some value like

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.