I'm getting a segmentation fault when I try to run my program. It's probably because I'm not referencing my pointer to a string right, or dereferencing whatever it's called. This is basically what my code looks like.

#include <stdio.h>

FILE *myFP;
char filename[20];
char *pstr = finlename;
void func(char *pstr);

int main(void) {
            scanf("Enter the filename: %s", &filename); 
            printf("The name of the file is %s", &pstr);
            function(pstr);
}

void func(char *str) {
               myFP = fopen(pstr, "r");
}

Recommended Answers

All 8 Replies

char *pstr = finlename; misspelling of filename

scanf("Enter the filename: %s", &filename); remove the & at filename

printf("The name of the file is %s", &pstr); remove the & at pstr

function(pstr); wrong function, it should be called func

char *pstr = finlename; misspelling of filename

scanf("Enter the filename: %s", &filename); remove the & at filename

printf("The name of the file is %s", &pstr); remove the & at pstr

function(pstr); wrong function, it should be called func

lol I typed this up so fast I didn't realize all the errors I made. However I really didn't know about not adding those &'s. Editing now.

>However I really didn't know about not adding those &'s.
When you use scanf or printf with the format operator %s ( format as string ) the & is not needed because filename is actually the address of the first element of filename ( filename[0] ), neither is necessary with pstr since is a pointer already, or it holds the address of some string.

My program is still broken. Now I'm getting the warning: passing arg 1 of `fread' makes pointer from integer without a cast

in my function it looks like

//add these to the list of previous variables
int num;
unsigned char buff;

func (char *pstr) {
    myFP = fopen(pstr, "r")
    if (myFP == NULL) {
         printf("cannot open file"\n);
         exit (0);
     }
     while(!feof) {
             num = fread(buff, 1, 1, myFP) - 48;  //line giving the warning
            // commented out stuff
     }
}

num = fread(buff, 1, 1, myFP) - 48; remove the -48 from it.

feof() function as the exit condition for a loop is not good. Read more here.

The reason I have the -48 there is to convert the char to an integer, that someone else here told me to do. I figured out that I needed to make the char buff a pointer by adding an asterisk and all my warnings went away but I'm still getting seg fault even when i remove the -48.

I also made while(!feof) {} to while(!feof(myFP) ) {} but I get the seg fault when i commented these out and just run the fread statement ONCE.

The reason I have the -48 there is to convert the char to an integer, that someone else here told me to do. I figured out that I needed to make the char buff a pointer by adding an asterisk and all my warnings went away but I'm still getting seg fault even when i remove the -48.

fread returns the number of items successfully read, not a specific character. Please look up the functions you use -- don't expect them to read your mind.

I also made while(!feof) {} to while(!feof(myFP) ) {} but I get the seg fault when i commented these out and just run the fread statement ONCE.

Unfortunately, that change probably ain't gonna help.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046476070&id=1043284351

I see you are struggling a little bit with fread. Would you understand how to interpret the prototype of a function?

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );


Read block of data from stream

Reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by ptr.
The postion indicator of the stream is advanced by the total amount of bytes read.
The total amount of bytes read if successful is (size * count).

Parameters

ptr
Pointer to a block of memory with a minimum size of (size*count) bytes.
size
Size in bytes of each element to be read.
count
Number of elements, each one with a size of size bytes.
stream
Pointer to a FILE object that specifies an input stream.

Return Value
The total number of elements successfully read is returned as a size_t object, which is an integral data type.
If this number differs from the count parameter, either an error occured or the End Of File was reached.
You can use either ferror or feof to check whether an error happened or the End-of-File was reached.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.