Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3424696
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:30:18+00:00 2026-05-18T06:30:18+00:00

#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { FILE *fp; fp=fopen(mydata.txt,r); if(fp==NULL) { perror(Error while opening);

  • 0
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
        FILE *fp;
        fp=fopen("mydata.txt","r");
        if(fp==NULL)
        {
                perror("Error while opening");
                exit(0);
        }
        char *s=(char*)malloc(100);
        while(feof(fp)!=EOF)
        {
                fscanf(fp,"%[^\n]",s);
                printf("%s",s);
        }
        return 0;
}

I am trying to read a file line by line.I am getting infinite loop.Where it has gone wrong ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:30:18+00:00Added an answer on May 18, 2026 at 6:30 am

    You can use the following code to do it.

    Your problems were that you weren’t checking the return from fscanf and that you weren’t actually reading the newline (so the next time you read, you wouldn’t go to the next line).

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int main (void) {
        FILE *fp;
        int x;
        fp=fopen("mydata.txt","r");
        if(fp==NULL) {
            perror("Error while opening");
            exit(0);
        }
        char *s=(char*)malloc(100);
        while(!feof(fp)) {
            x = fscanf(fp,"%[^\n]",s);
            fgetc(fp);
            if (x == 1)
                printf("%s\n",s);
        }
        return 0;
    }
    

    However, if all you’re after is the ability to input and process lines, fgets is a better solution than fscanf since there’s no chance of buffer overflow:

    #include <stdio.h>
    #include <string.h>
    
    #define OK       0
    #define NO_INPUT 1
    #define TOO_LONG 2
    static int getLine (char *prmpt, char *buff, size_t sz) {
        int ch, extra;
    
        // Get line with buffer overrun protection.
        if (prmpt != NULL) {
            printf ("%s", prmpt);
            fflush (stdout);
        }
        if (fgets (buff, sz, stdin) == NULL)
            return NO_INPUT;
    
        // If it was too long, there'll be no newline. In that case, we flush
        // to end of line so that excess doesn't affect the next call.
        if (buff[strlen(buff)-1] != '\n') {
            extra = 0;
            while (((ch = getchar()) != '\n') && (ch != EOF))
                extra = 1;
            return (extra == 1) ? TOO_LONG : OK;
        }
    
        // Otherwise remove newline and give string back to caller.
        buff[strlen(buff)-1] = '\0';
        return OK;
    }
    

     

    // Test program for getLine().
    
    int main (void) {
        int rc;
        char buff[10];
    
        rc = getLine ("Enter string> ", buff, sizeof(buff));
        if (rc == NO_INPUT) {
            printf ("No input\n");
            return 1;
        }
    
        if (rc == TOO_LONG) {
            printf ("Input too long\n");
            return 1;
        }
    
        printf ("OK [%s]\n", buff);
    
        return 0;
    }
    

    Sample runs with ‘hello’, CTRLD, and a string that’s too big:

    pax> ./qq
    Enter string> hello
    OK [hello]
    
    pax> ./qq
    Enter string>
    No input
    
    pax> ./qq
    Enter string> dfgdfgjdjgdfhggh
    Input too long
    
    pax> _
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *fp; char ch; char
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
#include<stdio.h> #include<zlib.h> #include<unistd.h> #include<string.h> int main(int argc, char *argv[]) { char *path=NULL; size_t size;
#include <stdio.h> #include <stdlib.h> #include <string.h> int main ( int argc, char *argv[] )
#include<stdlib.h> #include<string.h> #include<stdio.h> int pstrcmp( char **p,char **q) { return strcmp(*p,*q) ; } int
The following code outputs Illegal seek: #include <stdio.h> #include <errno.h> #include <string.h> int main()
#include <stdio.h> #include <stdlib.h> #include <string.h> char *readLine(FILE *inFile) //Simply reads line in a
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.