using fopen with const char array?

This is a discussion on using fopen with const char array? within the C Programming forums, part of the General Programming Boards category; Hey, I'm currently working on a C programming assignment but am stuck. I need to prompt a user to enter ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    8

    using fopen with const char array?

    Hey,

    I'm currently working on a C programming assignment but am stuck. I need to prompt a user to enter a filename then I must pass that filename to a function(getdata) and have the getdata function open the file and read the contents of the file.

    So im my main function I have:
    Code:
    int main(void){
    
    char filename[];
    
    printf("Enter data file:");
    scanf("%s",filename);
    getdata(filename);
    }
    and the getdata function looks like this:
    Code:
    data getdata(const char file[]){
    
    FILE *data;
    
    data = fopen(file[], "r");
    now at the moment I only need help to get the file to open, my problem is I know the first argument of fopen should not be "file[]" I have tested it and it does not work.

    Any idea's?

    P.S The getdata function prototype was provided by the tutor and can not be changed.

    Thanks

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    8
    I have done some more testing and managed to get it working. This is what I did, please let me know if my logic is correct.

    Im main I created a char pointer:

    Code:
    char file, *filename;
    filename = &file;
    printf("Enter data file name: ");
    scanf("%s",&file);
    getdata(filename)
    now this is going to send the address of file to the getdata function

    Code:
    data getdata(const char file[]){
    
    FILE *data;
    
    data = fopen(file, "r");
    now the first argument of open expects a char*, so I am passing the address of file to fopen for read access.

    Now if I can correct here why must the getdata function prototype (provided by tutor) be of type "const char file[]" couldnt it simply be "char *file"?

    Thanks is advance.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Im main I created a char pointer:
    your char pointer points to the storage big enough to store exactly 1 character.
    simple file name like
    "test1234.txt" needs 13 bytes to be stored (and there are filenames a lot longer than that), so your code makes no good
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You are completely mixing the types.
    To store the name of the file you just need an array of char (a string). That array of char you will pass to your getdata function. That is all:

    Code:
    char file[100];
    printf("Enter filename:");
    scanf("%s, file);
    getdata(file);
    Also, you say that getdata function returns a value of type data. Is data a type "created" with typedef? Or you want to return the variable data you declare? If that is the case then you should have:
    Code:
    FILE * getdata(const char file[]) {
       FILE *data;
       data = fopen(file, "r");
       ...
       return data;
    }
    since data has the type of FILE *

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    8
    I have made the suggested modifications. I am still having some trouble compiling this program to test the getdata function.

    Here is the main function
    Code:
    #include <stdio.h>
    #include "misc.h"
    
    int main(void){
    
    data *data_ptr;
    char file[100];
    
    //Prints student information to screen
    printinfo();
    printf("Enter data file:");
    scanf("&#37;s",file);
    data_ptr = getdata(file);
    
    return 0;
    }
    and here is the getdata function

    Code:
    data getdata(const char file[])
    {
    	int numLines = 0;
    	double x[50],y[50];
    	data *data_ptr;
    	char line[80];
    	FILE *file_data;
    	
    	//open file
    	file_data = fopen(file, "r");
    	if (file_data == NULL)
    		printf("Could not open file");
    		
    	//count number of lines in file
    	while ( fgets(line, 80, file_data) != NULL) numLines++;
    	
    	//allocate memory for structure
    	data_ptr = malloc(sizeof(data) * numLines);
    	
    	//initilize values for structure elements
    	for(int count = 0; count <= numLines; count++){
    		fscanf(file_data, "%lf %lf", &x[count], &y[count]);
    		*(data_ptr + count) = {numLines,x[count],y[count]};
    	}
    	
    	//return data structure pointer address to main
    	return data_ptr;
    }
    Im not sure how to initialize the values in the data structure to those values in the file.
    I have this line of code
    Code:
    *(data_ptr + count) = {numLines,x[count],y[count]};
    it doesnt work but what im trying to do is initialize the values in the structure to those values in the file and then return the value of the *data_ptr to main.

    The other compile time error I get is incompatible data type in main referenceing this line of code
    Code:
    data_ptr = getdata(file);
    Any help with this would be great.

    Thanks in advance.
    Last edited by traxy; 09-16-2008 at 05:16 AM.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    1) getdata() returns a type of data. ptr_data is a type of data *. You want this:
    Code:
    data * getdata(...)
    (you forget the *)

    2) You cannot assign values to a struct like that. Only when you initialize it. You need to do:
    Code:
    *(ptr_data+count).member1 = numLines;
    *(ptr_data+count).member2 = x[count];
    *(ptr_data+count).member2 = y[count];
    where member1/2/3 are the members of the struct data (the variables inside the struct, don't know how the struct is defined so don't know their names.
    You can also simply use ptr_data[count], which means exactly *(ptr_data+count)!
    Last edited by C_ntua; 09-16-2008 at 05:27 AM.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Note that
    scanf("&#37;s",file);
    has no protection over the memory overrun

    because your file array is 100 bytes (and we need a place for nul-terminator) better use

    scanf("%99s",file);
    Last edited by vart; 09-16-2008 at 07:50 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    8
    thanks heaps for your help.

    looks like I had to use although i would have though that * would take precedence over +
    Code:
                (*(data_ptr+count)).member1 = numLines;
                (*(data_ptr+count)).member2 = &x[count];
                (*(data_ptr+count)).member3 = &y[count];
    but when I had it like this:
    Code:
                (*data_ptr+count).member1 = numLines;
    I would get an error at compile time stating that "error: invalid operands to binary +"

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To avoid the complications of precedence here, you could use the array syntax:
    Code:
    data_ptr[count].member1 = ...
    I find that easier to read, and makes the syntax easier too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    You have another problem in getdata(). When you read the entire file to get a line count the file pointer stops at EOF and remains there. In order to load your structure array you must call either:

    rewind(file_data);

    or

    fseek(file_data,0,0);

    to put the file pointer back to the beginning of the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  4. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 02:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21