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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:28:17+00:00 2026-05-28T00:28:17+00:00

I have a big text file containing data that needs to be extracted and

  • 0

I have a big text file containing data that needs to be extracted and inserted into a new text file. I possibly need to store this data in an cell/matrix array ?

But for now, the question is that I am trying to test a smaller dataset, to check if the code below works.

I have a code in which it opens a text file, scans through it and replicates the data and saves it in another text file called, “output.txt”.

Problem : It doesn’t seem to save the file properly. It just shows an empty array in the text file, such as this " [] ". The original text file just contains string of characters.

%opens the text file and checks it line by line.
fid1 = fopen('sample.txt');
tline = fgetl(fid1);
while ischar(tline)
    disp(tline);
    tline = fgetl(fid1);
end
fclose(fid1);


% save the sample.txt file to a new text fie
fid = fopen('output.txt', 'w');
fprintf(fid, '%s %s\n', fid1);
fclose(fid);

% view the contents of the file
type exp.txt

Where do i go from here ?

  • 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-28T00:28:17+00:00Added an answer on May 28, 2026 at 12:28 am

    It’s not a good practice to read an input file by loading all of its contents to memory at once. This way the file size you’re able to read is limited by the amount of memory on the machine (or by the amount of memory the OS is willing to allocate to a single process).

    Instead, use fopen and its related function in order to read the file line-by-line or char-by- char.

    For example,

    fid1 = fopen('sample.txt', 'r');
    fid = fopen('output.txt', 'w');
    
    tline = fgetl(fid1);
    while ischar(tline)
        fprintf(fid, '%s\n', tline);
        tline = fgetl(fid1);    
    end
    
    fclose(fid1);
    fclose(fid);
    
    type output.txt
    

    Of course, if you know in advance that the input file is never going to be large, you can read it all at once using by textread or some equivalent function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big paragraph of text that is divided into subparagraphs with <br>
i have a big text file (like 1gb) and i need to insert line
I have a large text file with a lot of \n that I need
I have a big text file and the data in it are in 5
What if you have a big text file that has many entries in it
I have a big load of documents, text-files, that I want to search for
I have a big problem. I want to extract text from html table that
I have text file looks like that: float a[10] = { 7.100000e+000 , 9.100000e+000
I have a big text file of 13 GB with 158,609,739 lines and I
I have a text file which is tab separated. They can be quite big

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.