Thread: read file data:-

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    18

    read file data:-


    When an option is selected, the program will read the series.txt file data
    The data will be stored in memory into an array of pointers.
    In addition, the watch.txt file into which the watch data will be written will be re-initialized as a blank file.

    The size of the array is 1000, each cell in the array points to a structure of the following shape:
    Code:
    #include <stdio.h>
    Code:
    
    
    Code:
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct serie {
        char serieName[20];
        int code;
        int rank;
        int* watchingDetails;
    }serie;
    void main()
    {
    serie** sreees;
    
    
    sreees =getUnifyDatabaseDetails();
    free(sreees);
    }
    
    
    serie** getUnifyDatabaseDetails()
    {
        serie* temp = {"\0"};
        serie**  sreees = (serie**)malloc(1000);
        FILE* f3;
        int fur = 3, i;
        char name[20];
        int code, amount;
    
    
    
    
    
    
        f3 = fopen("series.txt", "r");
        for (i = 0; fur != EOF; i++)
        {
            fur = fscanf(f3, " %21[^,] ,%d ,%d", name,&code,&amount);
            strcpy(temp->serieName, name);
            temp->code = code;
            temp->rank = amount;
    
    
        }
    
    
        return sreees;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,750
    This is almost certainly wrong:
    Code:
    serie**  sreees = (serie**)malloc(1000);
    You want to malloc some multiple of sizeof(*sreees). Actually, because your instructions state that "The size of the array is 1000", you arguably shouldn't even use malloc here. Rather, I would change your function to something like this:
    Code:
    size_t getUnifyDatabaseDetails(series *series_list[], size_t size)
    Then call it like this:
    Code:
    #define SERIES_SIZE 1000 // SERIES_SIZE is the number of pointers to allocate
    /* ... */
    series *series_list[SERIES_SIZE] = {0};
    size_t series_count = getUnifyDatabaseDetails(series_list, SERIES_SIZE); // series_count is the count of the number of pointers in use
    In getUnifyDatabaseDetails, you will still use malloc, but it would be to allocate space for each series object, the address of which will be assigned to some pointer in series_list.

    Remember to free what you malloc (which means calling free for each series object allocated), and fclose what you fopen.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    i cant change my function :/

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,750
    As in you cannot change your function signature and return type? If so, then indeed you must malloc to allocate space for the pointers, but as I said you need to allocate 1000 * sizeof(*sreees). You still need to malloc for each pointer that will be used as well, and presumably set the unused pointers to be null pointers (or at least the first unused pointer, so you have a way of denoting the end of the portion of the array in use).

    What on earth is sreees though? Why not name it something like series_list?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    yeah i canot change the function signature and return type, sreees was changed to series_list thank you, i will try to done this.

  6. #6
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    Code:
    serie** getUnifyDatabaseDetails()
    {
    	serie* a;
    	serie **series_list;
    	FILE* f3, *f;
    	int fur = 3, i;
    	char name[20];
    	int linesnum=0;
    	int* forcode, *foramount;
    	a = (serie*)malloc(1000);
    	series_list= (serie**)malloc(1000);
    	f = fopen("watching.txt", "w");
    	fclose(f);
    	f3 = fopen("series.txt", "r");
    	for (i = 0; fur != EOF; i++)
    	{
    		fur = fscanf(f3, " %21[^\n]\n");
    		linesnum = linesnum + 1;
    	}
    	rewind(f3);
    	for (i = 0; i < linesnum; i++)
    	{
    		a[i].code=(int)malloc(sizeof(int));
    		a[i].rank= (int)malloc(sizeof(int));
    		a[i].serieName =(char) malloc(128*sizeof(char));
    		
    	}
    	for (i = 0; i < linesnum; i++)
    	{
    		fscanf(f3, "%s,",&a[i].serieName);
    		fscanf(f3, "%d,",a[i].code);
    		fscanf(f3, "%d,",a[i].rank);
    		a[i].watchingDetails = (int*)malloc(a[i].rank);
    		a[i].watchingDetails = &(a[i].rank);
    		series_list[i]->serieName = a[i].serieName;
    		series_list[i]-> rank= a[i].rank;
    		series_list[i]->code = a[i].code;
    		series_list[i]->watchingDetails = a[i].watchingDetails;
    	}
    
    
    
    
    	fclose(f3);
    	return series_list;
    }
    thats what i have done but i have a problem with that part
    Code:
    	for (i = 0; i < linesnum; i++)
    	{
    		fscanf(f3, "%s,",&a[i].serieName);
    		fscanf(f3, "%d,",a[i].code);
    		fscanf(f3, "%d,",a[i].rank);
    		a[i].watchingDetails = (int*)malloc(a[i].rank);
    		a[i].watchingDetails = &(a[i].rank);
    		series_list[i]->serieName = a[i].serieName;
    		series_list[i]-> rank= a[i].rank;
    		series_list[i]->code = a[i].code;
    		series_list[i]->watchingDetails = a[i].watchingDetails;
    	}
    can you please help me i have to submit my homework today :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-13-2013, 07:10 PM
  2. Replies: 21
    Last Post: 11-03-2007, 02:56 PM
  3. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  4. how do i read data from a file?
    By ssjnamek in forum C++ Programming
    Replies: 19
    Last Post: 02-01-2002, 04:58 PM
  5. read data out of *.txt file
    By dune911 in forum C Programming
    Replies: 3
    Last Post: 12-14-2001, 12:33 PM

Tags for this Thread