| |

VerySource

 Forgot password?
 Register
Search
View: 35|Reply: 7

How to read a line from a file at a time

[Copy link]

1

Threads

4

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-1-6 13:40:01
| Show all posts |Read mode
The file is a shell executable script.
The data read is required to conform to the rules of shell interpretation.
such as:
aaa\
bbb
Then the line read should be aaa bbb
Reply

Use magic Report

0

Threads

18

Posts

12

Credits

Newbie

Rank: 1

Credits
12

 China

Post time: 2020-1-6 17:27:01
| Show all posts
Don't understand the landlord
Reply

Use magic Report

1

Threads

4

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-1-6 18:12:01
| Show all posts
Don't understand the landlord
------------------------------------------
for example
For shell scripts, if the end of a line is\
Then it means that the line is not over yet.
Reply

Use magic Report

0

Threads

13

Posts

9

Credits

Newbie

Rank: 1

Credits
9

 China

Post time: 2020-1-7 01:45:01
| Show all posts
Your own explanation has defined the logic of the program. It's not difficult to implement, a scripting language like perl can read line by line and it's easy to handle your requirements.
If it is C, the simple way is to read the file into memory at one time, and you can handle it with a few pointers. The general code is as follows (note that the program is not debugged, there may be errors):
char * buffer;
int filelen;
char * buffer_end = buffer + filelen;
buffer = (char *) malloc (filelen);
/ * read file data to buffer * /

char * line_start;
char * line_end;
char line_buffer [1024]; / ​​* maximum line length is 1024 * /

line_end = line_start;

while (line_end <buffer_end) {
  int line_offset = 0;
  line_start = line_end;
  while (line_end! = '\n') line_end ++;
  if (line_end> = buffer_end) break;
  if (the last character before line_end is '\') {
    strncpy (line_buffer + line_offset, line_start, line_end-line_start);
    line_offset + = line_end-line_start; // determine here whether the line_buffer size is exceeded
  } else {
    Process line_buffer and empty
    strncpy (line_buffer + line_offset, line_start, line_end-line_start);
  }
}
Reply

Use magic Report

1

Threads

4

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

Post time: 2020-4-13 23:45:01
| Show all posts
fgets
Reply

Use magic Report

1

Threads

4

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-7-15 14:15:01
| Show all posts
\\means\
\as an escape character


The program can use c++, stl
As long as you know all the conditions of\escaping, it is not difficult to write a program.
But testing is troublesome. Is a big job
It is best to have a ready-made program.
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-7-17 20:30:01
| Show all posts
Clarified your needs, and there is nothing you can't achieve.
Reply

Use magic Report

1

Threads

4

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-7-28 12:15:01
| Show all posts
I have clarified my needs and nothing can't be achieved.
------------
It depends on the price. Do we want to make OS ourselves?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list