Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to read line by line from a file.

ssvasanssvasan Posts: 101Member
Hi,

I am working in the UNIX C, want to read information from a file, line by line. I tried the following code, but its always read first line indefinitely,

#include

void main()
{
char str[200];
FILE *fp;

fp = fopen("test.txt", "r");
while(! feof(fp))
{
fscanf(fp, "%[^
]", str);
printf("
%s", str);
}
fclose(fp);
}


- if you try with this code it prints only the first line indefinitely, so we have to press Ctrl+Break to stop the program. I want to read a whole line, immaterial of no of characters in the line.

Also i don't want to use the fgets().

thanks

- sri


Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2004-9-10 7:29:37[/red][/b][hr]
    use the fgets() function
    [code]
    : #include
    :
    : void main()
    : {
    : char str[200];
    : FILE *fp;
    :
    : fp = fopen("test.txt", "r");
    if(!fp) return 1; // bail out if file not found
    : while(fgets(str,sizeof(str),fp) != NULL)
    : {
    // strip trailing '
    ' if it exists
    int len = strlen(str)-1;
    if(str[len] == '
    ')
    str[len] = 0;
    : printf("
    %s", str);
    : }
    : fclose(fp);
    : }
    :
    [/code]

    : Hi,
    :
    : I am working in the UNIX C, want to read information from a file, line by line. I tried the following code, but its always read first line indefinitely,
    :
    : #include
    :
    : void main()
    : {
    : char str[200];
    : FILE *fp;
    :
    : fp = fopen("test.txt", "r");
    : while(! feof(fp))
    : {
    : fscanf(fp, "%[^
    ]", str);
    : printf("
    %s", str);
    : }
    : fclose(fp);
    : }
    :
    :
    : - if you try with this code it prints only the first line indefinitely, so we have to press Ctrl+Break to stop the program. I want to read a whole line, immaterial of no of characters in the line.
    :
    : Also i don't want to use the fgets().
    :
    : thanks
    :
    : - sri
    :
    :
    :



  • pseudocoderpseudocoder Posts: 687Member ✭✭✭
    : Hi,
    :
    : I am working in the UNIX C, want to read information from a file, line by line. I tried the following code, but its always read first line indefinitely,

    [blue]Try using while(fscanf(fp, "%s", str) == 1){ ... } [/blue]
    :
    : #include
    :
    : void main()
    : {
    : char str[200];
    : FILE *fp;
    :
    : fp = fopen("test.txt", "r");
    : while(! feof(fp))
    : {
    : fscanf(fp, "%[^
    ]", str);
    : printf("
    %s", str);
    : }
    : fclose(fp);
    : }
    :
    :
    : - if you try with this code it prints only the first line indefinitely, so we have to press Ctrl+Break to stop the program. I want to read a whole line, immaterial of no of characters in the line.
    :
    : Also i don't want to use the fgets().
    :
    : thanks
    :
    : - sri
    :
    :
    :
  • ssvasanssvasan Posts: 101Member
    Hi,

    This fscanf(fp, "%s", str) reads only one word at a time.But how should I read a line of information with help of fscanf.

    - sri.



    : : Hi,
    : :
    : : I am working in the UNIX C, want to read information from a file, line by line. I tried the following code, but its always read first line indefinitely,
    :
    : [blue]Try using while(fscanf(fp, "%s", str) == 1){ ... } [/blue]
    : :
    : : #include
    : :
    : : void main()
    : : {
    : : char str[200];
    : : FILE *fp;
    : :
    : : fp = fopen("test.txt", "r");
    : : while(! feof(fp))
    : : {
    : : fscanf(fp, "%[^
    ]", str);
    : : printf("
    %s", str);
    : : }
    : : fclose(fp);
    : : }
    : :
    : :
    : : - if you try with this code it prints only the first line indefinitely, so we have to press Ctrl+Break to stop the program. I want to read a whole line, immaterial of no of characters in the line.
    : :
    : : Also i don't want to use the fgets().
    : :
    : : thanks
    : :
    : : - sri
    : :
    : :
    : :
    :

  • pseudocoderpseudocoder Posts: 687Member ✭✭✭
    : Hi,
    :
    : This fscanf(fp, "%s", str) reads only one word at a time.But how should I read a line of information with help of fscanf.
    :
    : - sri.

    [blue]My understanding of fscanf is that it's the same as scanf in terms of breaking at the first white space character or new line. I tend to use fgets and sscanf when parsing input fields.[/blue]


    :
    :
    :
    : : : Hi,
    : : :
    : : : I am working in the UNIX C, want to read information from a file, line by line. I tried the following code, but its always read first line indefinitely,
    : :
    : : [blue]Try using while(fscanf(fp, "%s", str) == 1){ ... } [/blue]
    : : :
    : : : #include
    : : :
    : : : void main()
    : : : {
    : : : char str[200];
    : : : FILE *fp;
    : : :
    : : : fp = fopen("test.txt", "r");
    : : : while(! feof(fp))
    : : : {
    : : : fscanf(fp, "%[^
    ]", str);
    : : : printf("
    %s", str);
    : : : }
    : : : fclose(fp);
    : : : }
    : : :
    : : :
    : : : - if you try with this code it prints only the first line indefinitely, so we have to press Ctrl+Break to stop the program. I want to read a whole line, immaterial of no of characters in the line.
    : : :
    : : : Also i don't want to use the fgets().
    : : :
    : : : thanks
    : : :
    : : : - sri
    : : :
    : : :
    : : :
    : :
    :
    :
  • boushleyboushley Posts: 1Member
    Just in case anyone else stumbles across this, I was wondering the same thing about it reading the first line infinitely. I used the patter '
    %[^
    ]' and that did the trick instead of just '%[^
    ]'.
Sign In or Register to comment.