简体   繁体   中英

read from a file and store it in a variable c

i am trying to ready character by character from a file and store it in a variable. I require only first line of the file so i am using '\\n' or EOF to stop reading the character and i require spaces stored also . hear is my program , but i am getting warning while compiling like comparison between pointer and integer .. and when i run i am getting segmentation fault

#include<stdio.h>
#include<string.h>

void main()
{
  FILE *fp;
  char ch;
  char txt[30];
  int len;
  fp=fopen("~/hello.txt","r");
  ch=fgetc(fp);
  while(ch != EOF || ch!="\n")
  {
    txt[len]=ch;
    len++;
    ch=fgetc(fp);
  }
   puts(txt);
}

You're comparing to the wrong thing. Try:

ch != '\n'
      ^  ^

Also, as spotted in other answers, you're using len without initializing it.

Finally, you do realize fgets can do that as well. You could rewrite the thing to:

if (fgets(txt, sizeof txt, fp))
    ...

1) len is not initiated

int len=0;

2) From fgetc() page:

int fgetc ( FILE * stream );

so the fgetc() return int and not char so you have to define ch as int

int ch;

3) In addition of the cnicutar remark, the while condition should be checked with the && and not with || :

while(ch != EOF && ch!='\n')

4) You have to add null terminator charachter at the end of your txt buffer after finishing reading from file.

Add this line after the while loop

txt[len]='\0';

BTW you can read the first line with fscanf() it's more easier. Just use the following code

fscanf(fp, "%29[^\n]", txt);

The "%[^\\n]" means that fscanf will read all characters from fp except the '\\n' charachter and it will stop reading if it gets this charachter. So the fscanf will read all characters from fp till it find '\\n' character and save them into the buffer txt with null terminator charchter at the end.

The "%29[^\\n]" means that fscanf will read all characters from fp till it find '\\n' character or till it reach 29 readed charchters and save them into the buffer txt with null terminator charchter at the end.

len is not initialised so you're probably attempting to write way beyond the end of txt . The fix is simple - initialise it to 0 on declaration

int len = 0;

In addition to the error pointed out by cnicutar, you should also check the return value from fopen before using fp .

#include<stdio.h>
#include<string.h>

void main()
{
  FILE *fp;
  char ch;
  char txt[30];
  int len = 0;
  fp=fopen("~/hello.txt","r");
  if(!fp) {
    printf("Cannot open file!\n");
    return;
  }
  ch=fgetc(fp);
  while(ch != EOF && ch!= '\n' && len < 30)
  {
    txt[len] = ch;
    len++;
    ch=fgetc(fp);
  }
  txt[len] = 0;
  puts(txt);
}

This program may help you to solve your problem.

     #include<stdio.h>

     int main()
     {
       FILE *fp;
       int ch;
       char txt[300];
       int len=0;
       fp=fopen("tenlines.txt","r");

       do{
           ch=fgetc(fp);
           txt[len]=ch;
           len++;
         } while(ch!=EOF && ch!='\n');
     fclose(fp);
     puts(txt);

     return 0;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM