views:

408

answers:

6

Has any one ever seen a backspace delimited flat file? My requirement is to parse 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.

+1  A: 

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

Ignacio Vazquez-Abrams
+1  A: 

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

SLaks
Can you help me with such a script?
Harsha
What language do you want to write it in?
SLaks
I dont have any scripting language installed. Can I do it through java?
Harsha
Yes, you can. Write a Java program that opens a file and writes a string containing the escape code `\b` to the file.
SLaks
+1  A: 

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.

John Feminella
How is backspace a character? I would believe that backspace in fact deletes the character preceding it. Is the following string an example of backspace delimited string :apples/bbananas/bcoconut
Harsha
Backspace is what's called a control character. You're right in that it's not a printable character; it's simply a magic value that your file is using to delimit values. More on control characters here: http://en.wikipedia.org/wiki/Control_character
John Feminella
I went through the link. It seems that "\b" is a control character and not printable.But in your example "apples\bbananas\bcoconut" I am able to see "\b" and I can print it as well.
Harsha
@Harsha \b is the escaped version of backspace, like \n which represents a newline or \t which represents a tab. It's a method for writing characters which would otherwise be non-printable, or appear as whitespace.
meagar
Thanks. Somehow I was thinking that backspace in a file would look somewhat different than \bThis⌂is⌂backspace⌂delimitedI guess above is a backspace delimited file.
Harsha
@Harsha It may help to google ascii table and look at one of the results. The key is to remember that what matters is how you interpret the data, not what the data is. As John Feminella said it's just a magic value. You can interpret that value as deleting the previous character (normal use) or as a delimiter (what you want).
Andrew Myers
+1  A: 

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"
Brian Campbell
Thanks.Is it possible in Java? I use java
Harsha
Yes, it's possible in Java. But I don't really feel like writing in Java right now, and you should be able to translate the program. The same string literal syntax should work in Java: `"this\bis\nbackspace\bdelimited\n"`
Brian Campbell
I was able to write to a file through java.I used the following snippet:FileOutputStream out out = new FileOutputStream("C:\\myfile.txt");PrintStream p = new PrintStream( out );p.println ("This\bis\bwritten\bto\ba\bfile");Now when I open the file in notepad I can see:Thisiswrittentoafile (There is a box symbol between words which here I am not able to type)But if I do type myfile.txt from cmd shell I can see :ThiiwrittetfileAs we can see the last letter of each word has been deleted.Isnt this surprising? some data is getting lost?
Harsha
No, the data isn't getting lost. The command shell is interpreting the backspace characters, by deleting the previous character displayed, and then displaying the next one. Notepad, however, is not interpreting the backspace character, it's just displaying it as a box. So, you have generated a backspace delimited file appropriately; it seems to be working fine.
Brian Campbell
Thanks for the confirmation. Now that I have generated such a file, the small problem exists of parsing that file using bcp.
Harsha
+1  A: 

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

Drew Hoskins
Thanks this is really helpful.
Harsha
I tried this. But this looks different than writing to a file in java and using "\b" for backspace. The difference is that when I open such a file using windows cmd shell type command output comes likeThiiwrittetfile⌂jahswhere the character before j is actually entered using Ctrl+Backspace and the letter s in "this" is getting deleted because after s, I have put a \b through java code.
Harsha
Ctrl+Backspace doesn't actually create a backspace character. I was not able to parse such a character using \b
Harsha
+1  A: 

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.

JW