0

I am trying to copy a whole text file into char array using fstream but even upon increasing the size of the array it reads the text file to same limit .i am bount to save it in a char array and it will be good if it is not a dynamic one ??? any solution please ...

 // smallGrams.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include<iostream>
using namespace std;
#include<string>
#include<fstream>

void readInput(const char* Path);
 void removePunctucationMarks();
 void removeSpacing();
 void insertDots();
 char * getText();
 void generateUnigrams();
  void generateBigrams();
  void generateTrigrams();
 double validateSentance(string str);
 string sentenceCreation(int position);

 int main()
 {
      char *path="alice.txt";
      readInput(path);

     return 0;
 }
 void readInput(const char* Path)
 {
     ifstream infile;


     infile.open(Path);

     if(!infile.fail())
         cout<<"File opened successfully"<<endl;
     else
         cout<<"File failed to open"<<endl;

     int arrSize=100000000;
     char *arr=new char[arrSize];
     int i=0;
     while(!infile.eof()&&i<arrSize)
     {
         infile.get(arr[i]);
         i++;
     }
     arr[i-1]='\0';
     for(short i=0;i<arrSize&&arr[i]!='\0';i++)
     {
         cout<<arr[i];
     }





 }
  • What is wrong with `std::string`? – Yksisarvinen Sep 21 '18 at 11:42
  • @Yksisarvinen we are bound to use char array in the assignment – Mujeeb Alam Sep 21 '18 at 11:43
  • check by changing short to unsing int in the last for loop. – nayab Sep 21 '18 at 11:49
  • 1
    What are the contents of the file? What is the output? Have you debugged your code to see when exactly and under what conditions your loop ends? Do you know when the [`eof flag`](http://www.cplusplus.com/reference/ios/ios/eof/) is set and how [`get`](http://www.cplusplus.com/reference/istream/istream/get/) works? – Max Vollmer Sep 21 '18 at 11:51
  • Not directly related to yopur question, but if the file fails to be opened, it's not going to end well – Jabberwocky Sep 21 '18 at 12:10
  • 1
    _we are bound to use char array in the assignment_ You may send your teacher this link: [Kate Gregory: Stop Teaching C](https://www.youtube.com/watch?v=YnWhqhNdYyk) (may be, anonymously.) ;-) – Scheff's Cat Sep 21 '18 at 12:11
  • And you might have a problem here `for(short i=0;....` if `short` is a 16 bit type. – Jabberwocky Sep 21 '18 at 12:12
  • you need to create a version of vector. When the capacity is full allocate a new memory with larger size. – kelalaka Sep 21 '18 at 12:28
  • You should get the size of the file before creating the array. If you have a fixed capacity, you should use that to read the maximum characters. See `std::ifstream::read()`. – Thomas Matthews Sep 21 '18 at 14:30
  • BTW, your program continues to read a file if there was an issue opening it. – Thomas Matthews Sep 21 '18 at 14:36

3 Answers3

1

This is a C style solution that works. It checks the file size and then allocate the necessary memory for the array and reads all the content of the file in one call. The fread() call returns the number of bytes you requested or an error has ocurred (check fread() reference)

# include <cstring>
# include <cstdlib>
# include <cstdio>

int main(int argc, char *argv[]) {
    char *data;
    int data_len;
    FILE *fd;

    fd = fopen ("file.txt", "r");
    if (fd == NULL) {
        // error
        return -1;
    }

    fseek (fd , 0 , SEEK_END);
    data_len = ftell (fd);
    rewind (fd);

    data = (char *) malloc ((data_len + 1) * sizeof (char));
    memset (data, data_len + 1, NULL);

    if (fread (data, sizeof (char), data_len, fd) != data_len) {
        // error
        return -1;
    }

    printf ("%s\n", data);

    fclose (fd);
    free (data);

    return 0;
}
Marcelino
  • 331
  • 1
  • 10
-1

Here with a simple doubling method...

#include<iostream>
#include<string>
#include<fstream>
#include <cstdint>
#include <cstring>

using namespace std;

void readInput(const char* Path)
{
     ifstream infile;


     infile.open(Path);

     if(!infile.fail())
         cout<<"File opened successfully"<<endl;
     else{
         cout<<"File failed to open"<<endl;
         return;
     }

     int capacity=1000;
     char *arr=new char[capacity];
     char *temp;

     int i=0;

     while(infile >> arr[i])
     {
         i++;
         if ( i >= capacity ) {

             temp = new char[capacity*2];
             std::memcpy(temp , arr, capacity);
             delete [] arr;
             arr = temp;
             capacity *=2;
         }
     }
 }

int main()
{
    char *path="alice.txt";
    readInput(path);

    return 0;
}
kelalaka
  • 4,046
  • 4
  • 22
  • 39
-2

The error could when you read and display the array content using the for loop and not on reading the data from file. Use int instead of short in for loop, as short can increment upto 32768, only.

Krishna
  • 65
  • 4