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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:23:33+00:00 2026-05-31T20:23:33+00:00

I have a *.txt files having integers, one on each line. So the file

  • 0

I have a *.txt files having integers, one on each line. So the file would look something like

103123
324
4235345
23423
235346
2343455
234
2
2432

I am trying to read these values from a file line by line so I can put them in an array. Below is some code I wrote to achieve that

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int nArray[1000];
int i = 0;

int _tmain(int argc, _TCHAR* argv[])
{
    ifstream file("C:\Users\Chinmay\Documents\Array.txt");
    //fstream file("C:\Users\Chinmay\Documents\Array.txt", ios_base::out );
    //fstream file();
    //file.open("C:\Users\Chinmay\Documents\Array.txt", ios_base::out );

        bool b = file.is_open();
    //file.seekg (0, ios::beg);
    int i = file.tellg();
    while(!file.eof())
    {
        //string str;
        //getline(file, str);
                //nArray[i++] = atoi(str.c_str());
        char str[7] = {};
        file.getline(str,7);
        nArray[i++] = atoi(str);
    }
    file.close();
    return 0;
}

The file opens as the bool ‘b’ returns true. But the while loop exits in one run. and the array is empty. I looked up online and tried other things like the code given here at

code tutorial

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int nArray[100000];
int i = 0;

int _tmain(int argc, _TCHAR* argv[])
{
    ifstream in("C:\Users\Chinmay\Documents\Array.txt");
    bool b = in.is_open();

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  char str[255];

  while(in) {
    in.getline(str, 255);  // delim defaults to '\n'
    if(in) cout << str << endl;
  }

  in.close();

  return 0;

}

This returns immediately as well. The file opens but no data is read. The file is not empty and has the data in it. Could someone explain where I am going wrong? I am using Visual Studio 2011 beta.

  • 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-05-31T20:23:34+00:00Added an answer on May 31, 2026 at 8:23 pm

    This isn’t doing what you think it’s doing:

    ifstream file("C:\Users\Chinmay\Documents\Array.txt");
    

    Use forward slashes (even on Windows) and check the file open succeeded immediately:

    std::ifstream ifs("C:/Users/Chinmay/Documents/Array.txt");
    if (!ifs) 
    {
        // Failed to open file. Handle this here.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a txt file with 16 lines of integers. Each line contains 5
I have a .txt file having some text. I want to read this file
I'm having trouble with PHP text parsing I have a txt file which has
I have huge amount of txt files which contains 64x64 matrices consisting of integers.
i am having the script of tables in normal txt files also i have
I have a list of files stored in a text file called tempfile.txt. eg
I have a bunch of .txt files that have the following format: |file |
I have 30 txt files with data And I want to create on the
I have an activity that opens txt files. And everytime a user wants to
I have multiple UITextViews with corresponding .txt files. I'm reading them with NSString's stringWithContentsOfFile

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.