Thread: help in accesing .txt

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    Unhappy help in accesing .txt

    I need to access my .txt file then put it in my linked list so that I dont need to reinput all the datas again but im having problem with my algorithm help please

    Code:
    void openfiles(ptrClass *pFirst, ptrStudent *sFirst, int arr[])               
    {
         FILE *pFile;
         ptrClass pCurrent,pTemp,pNew;
         ptrStudent sCurrent,sTemp;
         string100 temp;
         
         pNew = malloc(sizeof(struct classmanagement));
         
         pNew = *pFirst;
         pCurrent = pNew;
         pFile = fopen("MP.txt", "rt");
         
         if(pFile == NULL)
          {
                   pFile = fopen("MP.txt", "wt"); 
                   }
         else
         {
           while(!EOF)
           {  
             
             
              fgets(temp, sizeof(struct classmanagement), pFile);
             
              strcpy(pTemp->coursecode,strtok(temp,"\t"));
              strcpy(pTemp->coursecode,strtok(NULL,"\t"));
              pTemp->section = atoi(strtok(temp, "\t"));
              pTemp->section = atoi(strtok(NULL, "\t"));
              pTemp->units = atoi(strtok(temp, "\t"));
              pTemp->units = atoi(strtok(NULL, "\t"));
              strcpy(pTemp->schedule,strtok(temp,"\t"));
              strcpy(pTemp->schedule,strtok(NULL,"\t"));
              pTemp->pTime.shours = atoi(strtok(temp, "\t"));
              pTemp->pTime.shours = atoi(strtok(NULL, "\t"));
              pTemp->pTime.smin = atoi(strtok(temp, "\t"));
              pTemp->pTime.smin = atoi(strtok(NULL, "\t"));
              pTemp->pTime.ehours = atoi(strtok(temp, "\t"));
              pTemp->pTime.ehours = atoi(strtok(NULL, "\t"));
              pTemp->pTime.emin = atoi(strtok(temp, "\t"));
              pTemp->pTime.emin = atoi(strtok(NULL, "\t"));
              strcpy(pTemp->lecturer,strtok(temp,"\t"));
              strcpy(pTemp->lecturer,strtok(NULL,"\t"));
              pTemp->slots =atoi(strtok(temp, "\n"));
              pTemp->slots =atoi(strtok(NULL, "\n"));
            
             pTemp->pNext = NULL;
             pNew = pTemp;
             pTemp = pTemp->pNext;
             pCurrent = pNew;
             pCurrent = pCurrent->pNext;
            
             }; 
             
             fclose(pFile);
             
             }
         
         
         }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What's the problem exactly?

    while(!EOF)

    No, EOF is a #define constant. Just test fgets() return value to test end of file

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    huh? sorry but I don't get that part

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    i'm not pretty sure on what's the problem but I know that the values are not being scanned

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Have you made any attempt at debugging this code? Either by adding printfs to check the values of the variables or by using an ACTUAL debugger like gdb?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    I have a menu to view class which includes the printfs but there's nothing on it and only shows NULL which means that the data inside the file is not scanned

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-30-2010, 08:04 PM
  2. Help converting CANoe logfile to EXCEL readable .txt file
    By turbofizzl in forum C Programming
    Replies: 21
    Last Post: 08-16-2009, 08:56 PM
  3. go to next line in .txt file
    By adr in forum C++ Programming
    Replies: 6
    Last Post: 03-21-2006, 10:23 PM
  4. pasting code form a .txt file...
    By Rune Hunter in forum C++ Programming
    Replies: 100
    Last Post: 09-10-2004, 04:54 PM
  5. Searching a .txt file?
    By Vladk10000 in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2004, 11:07 AM

Tags for this Thread