开发者

Backspace delimited flat files

开发者 https://www.devze.com 2022-12-15 07:18 出处:网络
Has any one ever seen a backspace delimited flat file? My requirement is to par开发者_如何学运维se such a file but I am not able to put a backspace character into a file to check if I am able to detec

Has any one ever seen a backspace delimited flat file? My requirement is to par开发者_如何学运维se such a file but I am not able to put a backspace character into a file to check if I am able to detect it.


Splitting shouldn't be any harder than using any other delimiter. It's just another character, after all. In Python, for instance:

>>> x = "apples\bbanana\bcoconut\bthese are delicious!"
>>> x.split('\b')
['apples', 'banana', 'coconut', 'these are delicious!']

Most languages use \b as the escape character for a backspace. If yours doesn't you can also include the ASCII control code for backspace itself, which is \x08.


I have never seen one, but some editors allow you to put a backspace character in by pressing e.g. Ctrl-V first.


You could write a script that appends the ASCII character code for backspace (\0x008) to a file.


Here is a C program that will generate you a backspace delimited file for testing (with newlines delimiting different rows). Pass in either a filename, or it will write it to stdout (I chose C because you didn't mention a platform; most people have a C compiler available):

#include <stdio.h>

int main(int argc, char **argv) {
  FILE *outfile;
  if (argc < 2)
    outfile = stdout;
  else
    outfile = fopen(argv[1], "w");

  fprintf(outfile, "this\bis\nbackspace\bdelimited\n");
  fclose(outfile);

  return 0;
}

The same string literal syntax should work in Java; I'll let you write the rest of the program:

"this\bis\nbackspace\bdelimited\n"


If using Windows, you can insert a backspace into notepad by using Ctrl+Backspace.


I would also recommend getting a hex editor like 0xED (for Mac). It's pretty useful for viewing and editing files containing unusual characters. With it, you can just type "08" to insert a backspace character into a file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号