Like Tree1Likes
  • 1 Post By dmh2000

stop reading until newline in file pointer

This is a discussion on stop reading until newline in file pointer within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { FILE *salesmanPtr; char buffer; salesmanPtr = fopen("salesman.txt","r"); while(salesmanPtr != "\n") { fscanf(salesmanPtr,"%c", &buffer); printf("%c\n",buffer); ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    stop reading until newline in file pointer

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	FILE *salesmanPtr;
    	char buffer;
    
    
    	salesmanPtr = fopen("salesman.txt","r");
    
    
    	while(salesmanPtr != "\n")
    	{
    		fscanf(salesmanPtr,"%c", &buffer);
    		printf("%c\n",buffer);
    	}
    
    
    }
    I want the reading process stop when reaching newline but salesmanPtr is a pointer. How can I modify this code to make it works?
    Attached Files Attached Files

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,497
    You are fairly close

    A few things
    -> main should be returning an integer at the end
    -> You should test to see where salesmanPtr is not equal to NULL before using it
    -> You should be using fclose (salesmanPtr) before the program ends

    Also, for future development - You should check to see if the "end-of-file" is reached before the new line character
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    uh, salesmanPtr is the FILE descriptor. it doesn't point at a character, 'buffer' contains the character you read. I would check buffer != '\n', not salesmanPtr, which doesn't change.
    Adak likes this.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You want to read the salesman.txt file. What do you want to do with the data you read in -- put it into a struct, put it into separate arrays, separate variables, or what?

    There's an easy way to do this, I believe, but I can't tell you what it is, because I don't know what you want done with the data yet.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,195
    It's a good idea to compile with warnings:
    Code:
    gcc -Wall -Werror -o program program.c
    Gives me this warning:
    program.c:10:23: error: comparison of distinct pointer types lacks a cast [-Werror]
    program.c:10:23: error: comparison with string literal results in unspecified behavior [-Werror=address]
    That will give you a big hint of where to look. Here is a fixed version:

    Code:
    #include <stdio.h>
     
    int main()
    {
        FILE *salesmanPtr;
        char buffer;
     
        salesmanPtr = fopen("salesman.txt","r");
     
        while(fscanf(salesmanPtr, "%c", &buffer) != EOF)
        {
            if (buffer == '\n')
                break;
            printf("%c\n",buffer);
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how can i stop having newline attached to input string?
    By Adam Rinkleff in forum C Programming
    Replies: 6
    Last Post: 07-03-2011, 10:10 AM
  2. reading newline
    By winsonlee in forum C Programming
    Replies: 4
    Last Post: 03-19-2004, 11:41 AM
  3. Stop reading from a file
    By Zalbik in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2002, 04:58 PM
  4. Reading data from a file till there is a newline?
    By mackol in forum C Programming
    Replies: 18
    Last Post: 06-27-2002, 10:22 PM
  5. stop read in newline
    By Supra in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 09:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21