Thread: Control Program for microcomputers simulator problem

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    13

    Control Program for microcomputers simulator problem

    The program first initialize disk, then creates and read file and delete file. I have no problem initializing and deleting But I'm having problem on reading the file that I created

    Create file function:
    Code:
    printf("Enter your file name: ");
        scanf("%s",filename);
        printf("Enter file extention: ");
        scanf("%s",filetype);
        filePos = openFile(filename,filetype);
        if(filePos==-1)
        {
            printf("File %s.%s created\n",filename, filetype);
            continue;
        }
     else
       {
        printf("Opened file %s.%s\n", directory[filePos].filename , directory 
        [filePos].filetype );
        continue;
       }
    Read file function:
    Code:
    if(filePos>-1)
    {
        printf("File name: %s.%s on bit: ",directory[filePos].filename, 
          directory  [filePos].filetype);
        for (i=0; i<16; i++)
        {
            printf("%d ,",directory[filePos].blocks [i]);
        }
        printf("\n");
        continue;
    }
    and my openFile function:
    Code:
    int openFile(char name[9],char type[9])
      {
    int i=0;
    for (i=0;i<32;i++)
    {
       if (strcmp(directory[i].filename, name)==0 && strcmp(directory
            [i].filetype, type)==0)
        {
            return i;
        }
     }
      for (i=0; i<32;i++)
     {
        if(strcmp(directory[i].filename,"")==0)
        {
            directory[i].usercode =1;
            strcat(directory[i].filename,name);
            strcat(directory[i].filetype, type);
            return -1;
        }
      }
      }
    The full program executes with no error, but just can't read the file

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Perhaps you have this accounted for in the flow of your program, but if you call openFile it doesn't always open the file -- if it creates the file, the file is not opened or at least the file handle is not returned, so if you go ahead and try to read without opening it again it will fail miserably.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Quote Originally Posted by tabstop View Post
    Perhaps you have this accounted for in the flow of your program, but if you call openFile it doesn't always open the file -- if it creates the file, the file is not opened or at least the file handle is not returned, so if you go ahead and try to read without opening it again it will fail miserably.
    Hey tabstop thanks for the quick reply, so you are suggesting that I open the file again in the "read file" function?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think I'm suggesting you should have your openFile function actually open the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help- quality control program
    By jccccc in forum C Programming
    Replies: 10
    Last Post: 01-21-2011, 10:42 PM
  2. Program Control (for loop)
    By naspek in forum C Programming
    Replies: 3
    Last Post: 01-28-2010, 06:25 PM
  3. Paging Program Simulator:
    By wise_ron in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 02:11 PM
  4. Program Control Help
    By mattz in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 10:10 PM
  5. Replies: 5
    Last Post: 09-08-2001, 09:18 AM