0

My text file contents are in the form "00101010111101101011".

I want to read it from the file and send each digit to an array,unfortunately i'm only able to use char while reading a file.How do i send each digit onto an array?a

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
#include <cstdlib>



using namespace std;
             
int main()
{
    
char st[32];
int y[32]={0},n=0;
int fstd=0;

ifstream std;
  
 
std.open("std.txt");
if(std.is_open())
{
    std.seekg(0, ios::end ); 
    fstd = std.tellg();
}

 std.read(st,fstd);
 std >> st[n];
 n++;
 std.close();
 cout <<endl;
 
 
 for(int i=0;i<=fstd;i++){  
 y= (unsigned char) st[i];        
 cout << st[i] << " ";
 }

  
  cin.get();
  return 0;
 
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by WaltP
0

Here's what I see:

std.open("std.txt");
   if(std.is_open())            // Why are you doing this?
   {                        
       std.seekg(0, ios::end ); // Jump to bottom of file
       fstd = std.tellg();  
   }
    
    // If open failed, why are you trying to read?
    std.read(st,fstd);    // You've jumped to the end of the file
    std >> st[n];         // -- there is nothing there to read
    n++;
    std.close();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.