E
E
Evgeny Petryaev2019-02-24 15:53:15
C++ / C#
Evgeny Petryaev, 2019-02-24 15:53:15

Read file block by block?

Here is the code you are looking for

FILE *fp,*fencrypted,*fdecrypted;
  char str[N];
  if ((fp=fopen("message.txt", "r" ))==NULL) {
    //printf("Cannot open file.\n");
    std::cout<<"Cannot open file.\n";
    exit (1);
  }
  
  std::vector<uint64_t> *msg = new std::vector<uint64_t>(),
    *plaintext = new std::vector<uint64_t>();//plain text

  unsigned long long id;
  while(!feof (fp)) {
    for (int i = 0; i<N; i++)
      str[i] = '\0';
    if (fgets(str, N, fp))
    {	
      //printf("%s", str);
      std::cout<<str;
      memcpy(&id, str, N);
      msg->push_back(id);
    }

here is the file
Business process, activities that produce a specific service or product for customers
Business process modeling, activity of representing processes of an enterprise in order 
Manufacturing process management, a collection of technologies and methods used to define.
Process architecture, structural design of processes, applies to fields such as computers.
Process costing, a cost allocation procedure of managerial accounting
Process management, ensemble of activities of planning and monitoring the performance of .
Process management (Project Management) , a systematic series of activities directed .
Process-based management, is a management approach that views a business as a collection .
Process industry, a category of material-related industry.

fgets is difficult to read (the last null character interferes), more precisely, split the entire text into arrays of 8 characters, you still need to score with zeros at the end if there are less than 8 characters

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Petryaev, 2019-02-24
@Gremlin92

Changed the code to fgetc seems to work

char ch;
  int countbyte = 0;
  while ((ch = fgetc(fp)) != -1)
  {
    if (countbyte % 8 == 0 && countbyte!=0)
    {
      countbyte = 0;
      std::cout << str;
      memcpy(&id, str, N);
      msg->push_back(id);
      for (int i = 0; i<N; i++)
        str[i] = '\0';
    }
    str[countbyte] = ch;
    countbyte++;
  }
  memcpy(&id, str, N);
  msg->push_back(id);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question