Thread: Update Files

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    Question Update Files

    Is this syntax C++?
    If it is how can i make it do the same thing in the C Language

    PHP Code:
    void studentClass::updateIndex(int idlong int pos)
    {
        
    indexType INDEX;
        
    ofstream indexFileINDEXFILEios::binary ios::app);

        
    INDEX.id id;
        
    INDEX.pos pos;
        if(!
    indexFile)
        {
            
    cerr << "File could not be opened." << endl;
            exit( 
    );
        }

        
    indexFile.write( (const char *)(&INDEX), sizeof(indexType) );
        
    indexFile.close();

    }
    //updateIndex 
    Last edited by nuddy; 04-06-2010 at 08:11 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Yes, it's C++. Use fopen/fclose and a FILE* object instead of the ofstream object and fwrite instead of the ofstream object's write member function. Instead of output with cerr you'd use fprintf with stderr. The function itself won't be a member function of any class/namespace so it will instead be a freestanding function. Other than that, it's the same, the basic structure will remain the same.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    Could u show me how can i upate this record from this function i have created?

    PHP Code:
    #include <stdio.h>
    #include <ctype.h>

    struct Student
    {
      
    int ID;
      
    char LastName[32]; 
      
    char FirstName[32];
      
    int DateOfBirth// This could be of the form YYYYMMDD. For example someone born on December 21st, 1990 would have a value of 19901221
      
    char Address[32];
      
    char TelephoneNumber[11]; // a 10-digit string
      
    char ProgramPursued[32];   
    };

    struct Student students[10]={
    {
    234,"Spencer""Sochelle"220587"29_Decent_Village""528-5214""Business"},         
    {
    345,"Dobson""Dwayne"890583"263_Far Park Blvd""457-2014""Computer"},
    {
    456,"Clarke""Dave"110181"87_Rasta_Village""354-5874""Law"},
    {
    567,"Currie""Nickeisha"230491"26_Waterground_rd""898-6578""Nurse"},
    {
    678,"Blackwood""Mariann"10490"3_St_Johns_road""256-1458""Art"},
    {
    789,"Hall""Wesley"101086"3_Bayfarm_road""236-1028""Science"},
    {
    891,"Hall""Bouki"251285"67_pimpim_drive""887-9910""Engineer"},
    {
    912,"Paul""Shawn"231180"25_glasco_Close""567-1996""Law"},
    {
    101,"Able""Frank"100591"1_Camp_road""528-1235""Business"},
    };

    char GetUserOption();
    int GetIDFromUser();
    void LetUserSearchForStudent();
    void UpdateStudents(Student students[]);
    void PrintStudents(Student students[]);
    void LoadStudents(Student students[]);
    void SaveStudents(Student students[]);


    char GetUserOption()
    {
      
    char option 'I'// 'I' for Invalid

      
    while(option == 'I')
      {
        
    // Print the menu items
        
    printf("\n");
        
    printf("Choose one of the following options:\n[u]pdate   [P]rint   [S]earch   [E]xit\n");
        
    scanf("%c", &option);

        switch(
    toupper(option))
        {
          case 
    'U':
          case 
    'P':
          case 
    'S':
          case 
    'E':
            break;
          default:
            
    option 'I';
            break;
        }
      }

      return 
    option;
    }

    int GetIDFromUser()
    {
      
    int id 0;
      
    printf("Enter the ID: ");
      
    scanf("%d", &id);
      return 
    id;
    }

    void LetUserSearchForStudent()
    {
      
    // Getting the ID from the user to search for
      
    int id GetIDFromUser();
      
    }





    // students must hold 10 students
    void UpdateStudents(Student students[])
    {
        





    // students must hold 10 students
    void PrintStudents(Student students[])
    {

    FILE *fp ;
    char ch ;

    fp fopen "records.txt""r" ) ;

    while ( 
    )
    {
    ch fgetc fp) ;

    if ( 
    ch == EOF )
    break ;

    printf "%c"ch ) ;
    }

    }


    // students must hold 10 students
    void LoadStudents(Student students[])
    {
      
    int i;
      
    FILE *fp;
      
    fp=fopen("records.txt","a+");
        
      for(
    010; ++i)
      {
        
    // Write the student to the file....here we will use comma-separated values
        
    fprintf(fp"%d,%s,%s,%d,%s,%s,%s\r\n"
                
    students[i].ID,
                
    students[i].LastName,
                
    students[i].FirstName,
                
    students[i].DateOfBirth,
                
    students[i].Address,
                
    students[i].TelephoneNumber,
                
    students[i].ProgramPursued
        
    );    
      }
    }



    // students must hold 10 students
    void SaveStudents(Student students[])
    {
      
    int i;

      
    // Open the file for writing
      
    FILE *fp fopen("records.txt""wb");
      if(
    fp == NULL) return; 

      
    // Loop through each student
      
    for(010; ++i)
      {
        
    // Write the student to the file....here we will use comma-separated values
        
    fprintf(fp"%d,%s,%s,%d,%s,%s,%s\r\n"
                
    students[i].ID,
                
    students[i].LastName,
                
    students[i].FirstName,
                
    students[i].DateOfBirth,
                
    students[i].Address,
                
    students[i].TelephoneNumber,
                
    students[i].ProgramPursued
        
    );    
      }

      
    // Close the file
      
    fclose(fp);  
    }


    int main()
    {
      
      
    int looping 1;

      
    // Load the students from the file
      
    LoadStudents(students);

      
    // Loop until exit
      
    while(looping)
      {
        
    char option GetUserOption();
        switch(
    option)
        {
          case 
    'U':
            
    UpdateStudents(students);
            break;

          case 
    'P':
            
    PrintStudents(students);
            break;

          case 
    'S':
            
    LetUserSearchForStudent();
            break;

          case 
    'E':
            
    looping 0// exit the loop
            
    break;
        }
      } 

      
    // Save the students to the file
      
    SaveStudents(students);

      return 
    0;


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. query problem
    By arian in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  3. Url query encoding/decoding
    By Niara in forum Networking/Device Communication
    Replies: 6
    Last Post: 04-25-2007, 03:30 PM
  4. DNS Query
    By Simpsonia in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-24-2006, 12:42 AM
  5. dns query
    By bulldog in forum C Programming
    Replies: 6
    Last Post: 02-24-2004, 10:44 AM