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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:01:45+00:00 2026-05-14T03:01:45+00:00

I am reading in a file into an array. It is reading each char,

  • 0

I am reading in a file into an array. It is reading each char, the problem arises in that it also reads a newline in the text file.

This is a sudoku board, here is my code for reading in the char:

bool loadBoard(Square board[BOARD_SIZE][BOARD_SIZE])
{
  ifstream ins;

  if(openFile(ins)){

    char c;

    while(!ins.eof()){
      for (int index1 = 0; index1 < BOARD_SIZE; index1++)
        for (int index2 = 0; index2 < BOARD_SIZE; index2++){ 
          c=ins.get();

          if(isdigit(c)){
            board[index1][index2].number=(int)(c-'0');
            board[index1][index2].permanent=true;
          }
        }
    }

    return true;
  }

  return false;
}

like i said, it reads the file, displays on screen, just not in correct order when it encounters the \n

  • 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-14T03:01:45+00:00Added an answer on May 14, 2026 at 3:01 am

    Well in your file format you can simply not save newlines, or you can add a ins.get() the for loop.

    You could also wrap your c=ins.get() in a function something like getNextChar() which will skip over any newlines.

    I think you want something like this:

     for (int index1 = 0; index1 < BOARD_SIZE; index1++)
     {
      for (int index2 = 0; index2 < BOARD_SIZE; index2++){
    
       //I will leave the implementation of getNextDigit() to you
       //You would return 0 from that function if you have an end of file
       //You would skip over any whitespace and non digit char.
       c=getNextDigit();
       if(c == 0)
         return false;
    
       board[index1][index2].number=(int)(c-'0');
       board[index1][index2].permanent=true;
      }
     }
     return true;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading the contents of the file using fread into an char array.
I'm reading a multi-dimensional char array from a file char pszBillToAddress[3][31]; Each row of
I have the following simple code, that reads contents of a text file into
What would be the most efficient method of reading a text file into a
I'm reading a binary file and storing each record into a byte[] . Now
I am reading a char array from a file in and then converting it
I'm trying to read a text file that looks like this 0,-16,-4,12,10,4,-14,8,44,8,8,12,-4 1,-16,-4,12,10,4,-14,6,43,10,10,12,-4 2,-16,-6,10,11,2,-13,4,43,10,11,12,-4
I have a large file(60mb) and I am reading the file into a string
I am reading a binary file into a parsing program. I will need to
I am reading an XML file into a DataSet and need to get the

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.