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 9214527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:59:19+00:00 2026-06-18T01:59:19+00:00

Noobie Alert. Ugh. I’m having some real trouble getting some basic file I/O stuff

  • 0

Noobie Alert.
Ugh. I’m having some real trouble getting some basic file I/O stuff done using <stdio.h> or <fstream>. They both seem so clunky and non-intuitive to use. I mean, why couldn’t C++ just provide a way to get a char* pointer to the first char in the file? That’s all I’d ever want.

I’m doing Project Euler Question 13 and need to play with 50-digit numbers. I have the 150 numbers stored in the file 13.txt and I’m trying to create a 150×50 array so I can play with the digits of each number directly. But I’m having tons of trouble. I’ve tried using the C++ <fstream> library and recently straight <stdio.h> to get it done, but something must not be clicking for me. Here’s what I have;

#include <iostream>
#include <stdio.h>
int main() {

const unsigned N = 100;
const unsigned D = 50; 

unsigned short nums[N][D];

FILE* f = fopen("13.txt", "r");
//error-checking for NULL return

unsigned short *d_ptr = &nums[0][0];
int c = 0;
while ((c = fgetc(f)) != EOF) {
    if (c == '\n' || c == '\t' || c == ' ') {
        continue;
    }   
    *d_ptr = (short)(c-0x30);
    ++d_ptr;
}   
fclose(f);
//do stuff
return 0;
}

Can someone offer some advice? Perhaps a C++ guy on which I/O library they prefer?

  • 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-18T01:59:20+00:00Added an answer on June 18, 2026 at 1:59 am

    Here’s a nice efficient solution (but doesn’t work with pipes):

    std::vector<char> content;
    FILE* f = fopen("13.txt", "r");
    // error-checking goes here
    fseek(f, 0, SEEK_END);
    content.resize(ftell(f));
    fseek(f, 0, SEEK_BEGIN);
    fread(&content[0], 1, content.size(), f);
    fclose(f);
    

    Here’s another:

    std::vector<char> content;
    struct stat fileinfo;
    stat("13.txt", &fileinfo);
    // error-checking goes here
    content.resize(fileinfo.st_size);
    FILE* f = fopen("13.txt", "r");
    // error-checking goes here
    fread(&content[0], 1, content.size(), f);
    // error-checking goes here
    fclose(f);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a noobie problem getting my head round how I can make this
I'm having a noobie problem getting my head round how I can make this
excuse what seems like a real noobie question but how can I implement the
Caution, WCF noobie alert I need to create a WCF client to query a
Thanks for your help on my last noobie jquery question and since you done
I am an ASP MVC 3 noobie who has done a few tutorials. Now
So, the noobie question of the day... I have an XML file with the
Noobie question... Using IIS7, I am trying to create a virtual directory for the
This may be a noobie questions, but... So I have a pyc file that
having another problem with perl and regex, sorry for being a badass perl noobie

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.