1

I want to find the position of a '\n' in a file and print out the remaining characters after \n

I've tried to use lseek to find the number of times '\n' occurred but I cannot seem to find the position at which the special character '\n' occurs

I've also tried using strok to break the particular file down and lseek to find the position of the last character

#define MAX_BUFFER_LENGTH 512
//path name is the file that is being accessed
void printLines(char *initPathName, char *initBuffer, int initnumberRead)
{   
int fileDescriptor; 
int numOfBreaks =0;

char *pathName=initPathName;
fileDescriptor = open(pathName, O_RDONLY);

lseek(fileDescriptor,initnumberRead * -1, SEEK_END); 

int size =  read(fileDescriptor, initBuffer, MAX_BUFFER_LENGTH);
initBuffer[size] = '\0';
char *token = strtok(initBuffer, "\\\n\r"); 

for(int i= 0;token !=NULL;i++)
{
    write(2,token, strlen(token));
    write(2,"\n", 2);
            write(2,"a break occured\n", 17);
    token = strtok(NULL,"\\\n\r"); 
}
close(fileDescriptor);
}

input

./program some.txt -l 3 // this prints the number of lines to be printed and separated with line breaks 

What is should do

three lines printed

some text 
a break occured
some text 
a break occured
some text 
a break occured

output

instead it prints the number of characters in the file and formats it based on when a \n occurred

.. ext 
a break occured
alfie
  • 157
  • 10
  • 2
    I think you are thinking about this somewhat backwards. Every line in a file will be terminated with `'\n'`. So rather than searching for a `'\n'`, you can simply read the file with `fgets` and a sufficient sized buffer, or use POSIX `getline` and let it allocate and simply count lines discarding however many you want to skip and keeping the lines you want. If you want the character count up to the `'\n'`, you can simply read a line and use `strcspn (buffer, "\n");` – David C. Rankin Aug 17 '19 at 04:30
  • 1
    Note that `strtok(initBuffer, "\\\n\r");` looks for any of 3 characters — backslash, newline, carriage return. Also: `write(2,"a break occured\n", 17);` writes a null byte to standard error (and "occurred" has two r's). – Jonathan Leffler Aug 17 '19 at 04:43
  • 1
    I think you need to show an MCVE ([Minimal, Complete, Verifiable Example?](https://stackoverflow.com/help/mcve)) (or MRE or whatever name SO now uses; MCVE was good for over five years and it did not need changing) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). See also [How to Ask Questions the Smart Way](http://www.catb.org/~esr/faqs/smart-questions.html) and [Writing the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). The calling code, and `MAX_BUFFER_LENGTH` should be shown too. – Jonathan Leffler Aug 17 '19 at 04:58
  • @JonathanLeffler alright thank you, I'll fix up my question. – alfie Aug 17 '19 at 05:04

1 Answers1

3

I want to find the position of a '\n' in a file and print out the remaining characters after \n

#include <stdio.h>

long long find_first_newline_then_print(FILE *fn) {
  int ch;
  long long pos = 0;
  while ((ch = fgetc(fin)) != EOF && ch != '\n') ++pos;
  if (ch == EOF) return -1;
  while ((ch = fgetc(fin)) != EOF) fputc(ch, stdout);
  return pos + 1;
}
Gene
  • 46,253
  • 4
  • 58
  • 96