Hi

I'm running this on a mac and when i use '\n' it prints the entire contents of the pointed to text file. looking for '\r' just cause an infinite loop. Any idea what the problem might be?

#include <stdio.h>
#include <stdlib.h>

int main(void){
	FILE * input;
	char * nlptr;
	long position;
	long count;
	char ch;
	int name[81];

	fprintf(stdout, "please enter a text file\n");
	fgets(name, 81, stdin);
	nlptr = strchr(name, '\n');
	if(nlptr)
		*nlptr ='\0';
	input = fopen(name, "r");
	printf("enter a file position\n");
	fscanf(stdin, "%d", &position);
	while(getchar() != '\n')
		continue;
	for(count = position; getc(input) != '\n'; count++){
		fseek(input, count, SEEK_SET);
		ch = getc(input);
		putc(ch, stdout);
	}
	
	fclose(input);
	printf("\n");
	return 0;
}

Thanks to anyone who can help

So you want to go to a point in the file, then what?
- print the rest of the file
- print a single line?

calling getc() TWICE in the loop means you're processing alternate characters.

You should also move the fseek() outside the loop,. you only need to do it once.

jcollins85,
missing end of file check.

for(count = position; getc(input) != '\r' && !feof(input); count++){
  fseek(input, count, SEEK_SET);
  ch = getc(input);
  putc(ch, stdout);
}

I remember MAC being either '\r' or '\n' only, while PC is definitely '\r' AND '\n' every time a '\n' is encoded!

'\n' in C program is standard across platforms, and the underlying implementation code (standard C libraries) translates it to whatever the operating system uses. It can be "\r\n" on MS-Windows, "\n" on *nix, "\r" in MAC, or anything else on other operating systems. For files opened in text mode all you need to do is specify '\n'. When files are opened in binary mode no conversions are made, so you have to know what os you are working with in order to translate them yourself.

Thanks for the input but I still can't get it to read to first newline. The goal is to only print the text from the the point given by fseek to the first newline. The solutions given read to the end of file.

Thanks for the input but I still can't get it to read to first newline. The goal is to only print the text from the the point given by fseek to the first newline. The solutions given read to the end of file.

The solutions given are not supposed to be an implementation of your assignment.

No plea for a solution here, just letting the previous posters know that I had already given their solutions a shot. I just don't know why it the code I gave won't read in the newline at the end of a line. Any explanations (no code necessary) of why this is occurring would be great.

This works ok for me

int main(void){
	FILE * input;
	char * nlptr;
	long position;
	long count;
	char ch;
	char name[81];

	fprintf(stdout, "please enter a text file\n");
	fgets(name, 81, stdin);
	nlptr = strchr(name, '\n');
	if(nlptr)
		*nlptr ='\0';
	input = fopen(name, "r");
	printf("enter a file position\n");
	fscanf(stdin, "%d", &position);
	while(getchar() != '\n')
		continue;
	fseek(input, position, SEEK_SET);
	for(count = position; (ch = getc(input)) != '\n'; count++){
		putc(ch, stdout);
	}
	
	fclose(input);
	printf("\n");
	return 0;
}

Maybe its just my implementation that causes this to fail (macbook pro, leopard). will keep experimenting. Thanks for all your input!

This article has been dead for over six months. Start a new discussion instead.