I have a text file (*.txt) in which I have certain lines starting with a space, I want to remove all the leading spaces. The text has almost 20k lines, out of which certain random lines are having a space at the start. Because of which I having difficulty in reading these lines. I want to remove the leading spaces so that it can be read properly.
Try using the following code:
inputFileID=fopen('input.txt','r'); % Open input file for reading
outputFileID=fopen('output.txt','w'); % Open output file for writing
formatted_lines_in_cell_array = textscan(inputFileID,'%s','Delimiter','\n'); % Scan input file, and split to rows
formatted_lines=formatted_lines_in_cell_array {1,1}; % Extract the formatted lines array from the 1x1 cell array output of textscan
fprintf(outputFileID,'%s\r\n',formmated_lines{:}); % Write formatted lines to output file
fclose(inputFileID); % Close files
fclose(outputFileID);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With