How to read line by line from a file. - Programmers Heaven

Howdy, Stranger!

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

Categories

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: 688Member ✭✭✭
    : 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: 688Member ✭✭✭
    : 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.