Hello,

I am a new user to Daniweb. My regards to all.

Well my C skills have taken a lot of rust and I am stuck at a small issue.
I want to read a data file containing some information. Now I want to read 2 integers from this file, add them and store them in a character array. I need this array for other filename.

For a start, I am trying to read only the first integer, stored at position 10 on the first line. But I am getting segmentation fault when I execute. Can some one point out whats wrong ?
Here is the code:

#include<stdio.h>
#include<stdlib.h>

main()
{
  FILE *fp;
  int buffer;
  char c;

  fp = fopen("mfdn.dat", "rb+" );

  fseek(fp,10,SEEK_SET);
  fread(buffer, sizeof(buffer), 1, fp);

  printf("buffer is : %c\n", buffer );
  fclose(fp);

}

I get this error:

filename.c: In function `main':
filename.c:13: warning: passing arg 1 of `fread' makes pointer from integer without a cast
Segmentation fault

Please advise me on this.

Cheers

The first parameter to fread() must be a pointer. What you are attempting to pass is an integer. Use the pointer & operator and it should fix that. That is inteded to read the binary version of an integer, not the text version. If the file is a text file (that is, a file that is easily readable by Notepad.exe or some other text editor) then you don't want to use fread() at all. Post an example of the file so we can tell you how it should be read.

>warning: passing arg 1 of `fread' makes pointer from integer without a cast
It's generally a good idea to fix your warnings. In this case, check the documentation on fread and you'll notice that it takes a pointer to void as the first argument, not an int.

The first parameter to fread() must be a pointer. What you are attempting to pass is an integer. Use the pointer & operator and it should fix that. That is inteded to read the binary version of an integer, not the text version. If the file is a text file (that is, a file that is easily readable by Notepad.exe or some other text editor) then you don't want to use fread() at all. Post an example of the file so we can tell you how it should be read.

Sorry about that. I did try char but was just trying int to remove the warning. Anyway, I think fread is not a good idea since the file I am reading is a text file: Here are the contents:

11 6 # shells, # protons
11 6 # shells, # neutrons
3 Number of multi-particle state symmetries (N-hbar-omega, M, PARITY)
3 0 1 0 2 1 Sym. type, N_max, used: (0,1)=(N,Y), N_min, del, #
0 0 1 1 1 1 Sym. type, Total M_j, value, used
2 1 1 1 1 1 Sym.type, PARITY, (+,-)=(1,2), used
400 1 # Lanczos iters, ref state for transitions
2 16 0.0 initpiv, ncud, matrix element cut
0 1 IREST, # Hams

I want to read the integers in column 10 in the first 2 lines(the protons and neutron numbers). Please advise how this should be read.

Thanks a lot.

Cheers

Have you tried fscanf? :icon_rolleyes:

Hi,

I tried fscanf and it works better. I am now able to scan the first integer:
this is mu code:

#include<stdio.h>
#include<stdlib.h>

main()
{
  FILE *fp;
  char filename[8];
  char c;
  int prot,neu;

  fp = fopen("mfdn.dat", "rb+" );

  fseek(fp,4,SEEK_SET);

  fscanf(fp,"%d",&prot);
  printf("prot is : %d\n", prot);

  buffer[0] = prot;
  printf("buffer is : %c\n", buffer[0]);
  fclose(fp);

}

Now how to i go to the beginning of the next line so that I can read the next integer ? I tried adding 80 to the seek offset but that prints garbage. Also, how do i store this integer as a char, since I need these numbers in a character array.

buffer[0] = prot does not work. I tried typecasting as
buffer[0] = (char)prot, but this prints garbage as well.

Cheers

I changed the following lines and now i can scan the integer as a character:

fseek(fp,9,SEEK_SET);

fscanf(fp,"%c",&c);

However, i dont know how to go the beginning of the next line using fseek

why not just use fgets() to get the entire line then you can use strtok() or some other method to extract the data. fgets() will automatically go to the next line without you having to do anything extra.

// this will read the entire file.
char buf[255];
while( fgets(buf, sizeof(buf), infp) )
{
    // do something with this line
}

I was about to suggest to use fgets function, but AD had already have a post on it. Using a fscanf function is also a good idea.

But only other thing which you might have to do if your using fgets is that, you will have to token the string and then convert that from string to an integer.

But when you use fscanf, you don't have to worry anything about type casting, since the fscanf function will do it for your, provided if you read the value as an integer.

Using fseek is more complicated and normally used when files are open in "b" mode. More over, it quite often used to read chunk of data like whole record or a structure. For example student records 1 and so on.

It would a good start if you could start reading it through fscanf or fgets and make your way through to try reading it through fread.

ssharish

Parsing lines of text like that is pretty mean work if you are just using C string routines such as strtok(). C++ string classes always have string handling/parsing routines similiar to those found in BASIC dialect languages that make this sort of work pretty easy.

>C++ string classes always have string handling/parsing routines similiar to
>those found in BASIC dialect languages that make this sort of work pretty easy.
First, this is the C forum, not the C++ forum. Second, much of C++'s string functionality is available in the form of C functions if you know where to look. Finally, even C++ sucks much ass when it comes to string handling.

Comments
That last sentence was brilliantly succinct. :icon_razz:

Hello All,

Thanks for all ur suggestions....I finally got the work done to some extent....

I now read the lines 1 and 2 in buffers and then do a sscanf to read the integers.....then do the required additon and store it back using sprintf.....

Thanks for all the help.....


this is my code:

fp = fopen("mfdn.dat", "rb+" );

  fgets(buf, sizeof(buf), fp);
  sscanf(buf,"%d %d",&shells,&prot);
  fgets(buf, sizeof(buf), fp);
  sscanf(buf,"%d %d",&shells,&neu);
  i = prot + neu;
  printf("i is : %d, prot is: %d and neu is %d\n",i,prot,neu);
  sprintf(smfilename,"%d_%d",i,shells);
  printf("smFilename is:%s",smfilename);
  i=0;
  while(smfilename[i]!='\0')
    {
      printf("\nsmfilename[%d] = %c\n",i,smfilename[i]);
      i++;
    }

  fclose(fp);
This article has been dead for over six months. Start a new discussion instead.