Probs: Einlesen/Ausgabe einer Ascii Datei mit "^M" terminierten Zeilen



  • Hi Leute!

    Ich habe folgendes Prob. Möchte ein Datei einlesen und zeilenweise ausgeben. Die Datei wurde scheinbar mit einem nicht standardkonformen Editor erstellt. Wo ein \n Newline stehen sollte steht ein ^M Zeichen.

    Zu Ausgabe benutze ich ncurses. Das funktioniert wunderbar, ausser mit einer Datei deren Zeilen auf diese Weise terminiert werden.

    Über Hilfe würde ich mich sehr freuen.

    mfg
    tom

    entsprechende Problemzeilen im Quellcode meines Progs:

    while( ((int) (c = fgetc(file))) != EOF) { //read data from the file until the end; EOF = End Of File
          addch(c); // put a single character on the screen
          currentCol++; // everytime we put a character on screen, we must increment the columnIndex currentCol, to know our current pos on screen 
    
          if( c == '\n') { //if we have just read a Newline Symbol '\n', we start again on the very left position on the screen ...
    	currentCol = 0; // ...so we must update our currentCol indexvariable to 0 ...
    	linesToDisplay++; //  ...and we start in the next line, we also have to update this index
          }
          if(currentCol == COLS) { //in this case we have reached the very right of the screen, we cannot go on printing text on the screen, hint: the user won't see it.
    	linesToDisplay++; // so we increment again the lineIndexVariable...
    	move(linesToDisplay, 0); // ...move the cursor to the new line and the column 0...
    	currentCol = 0; //...and we update our currentColIndexVariable.
          }
    
          if(linesToDisplay == LINES) { //if we reach the bottom of the page
    	refresh(); //print the data into the ncurses window, NOT the new page's data !!
    	//now we must initalize a new window, clear the screen put the cursor to the upper left corner, and go on printing text on the screen
    	linesToDisplay = 1; //we start again with a page so we start in the first line
    	move(0,0); //move printing cursor the upperleft corner
    
    	char input = getch(); //wait until the user hits any key
    	if( input == 'q') { //quit program if the user presses the q key
    	  quit(file);
    	  exit(1);
    	}
    	clear(); //clear the screen
          }
        } //end while loop
    


  • nicht zeilenweise sonder zeichenweise ! 😃



  • Alles klar ^M steht für \r

    mfg
    tom



  • jo ^M ist \r. \r wird unter Mac OS (vermutlich bis auf 😵 benutzt um Zeilen zu beenden.



  • Und vor allem unter Windows. Deswegen gibts ja auch das schöne Tool dos2unix 🙂



  • Wenns nur um \r\n Vs. \r Vs. \n geht dann kann man das auch via sed oä lösen...


Anmelden zum Antworten