C programming!!!
Results 1 to 7 of 7

Thread: C programming!!!

  1. #1

    Talking C programming!!!

    I need to know how to print to data files.
    I know there's fprintf and fputs but not sure how to read it back in for a report display


    here is my code so far...

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    void Menu();
    void Create();
    void Add();
    void Edit();
    void Delete();
    void DisplayS();
    void DisplayC();

    int main()
    {
    Menu();

    return 0;
    }

    void Menu()
    {
    int choice = 0;

    while (choice != 7)
    {
    system("cls");
    printf("\nWELCOME TO GRADE MASTER !");
    printf("\n*****************************");
    printf("\n* *");
    printf("\n* 1. CREATE STUDENT FILE *");
    printf("\n* *");
    printf("\n* 2. ADD STUDENT FILE *");
    printf("\n* *");
    printf("\n* 3. EDIT STUDENT FILE *");
    printf("\n* *");
    printf("\n* 4. DELETE STUDENT FILE *");
    printf("\n* *");
    printf("\n* 5. DISPLAY STUDENT DATA *");
    printf("\n* *");
    printf("\n* 6. DISPLAY CLASS DATA *");
    printf("\n* *");
    printf("\n* 7. END PROGRAM *");
    printf("\n* *");
    printf("\n*****************************");
    printf("\nRun which option ( 1 - 7 ) ?");

    scanf("%d", &choice);
    fflush(stdin);

    switch (choice)
    {
    case 1: Create(); break;
    case 2: Add(); break;
    case 3: Edit(); break;
    case 4: Delete(); break;
    case 5: DisplayS(); break;
    case 6: DisplayC(); break;
    case 7: break;
    }
    }
    }

    void Create()
    {
    int pauser;
    FILE *ptr_file;
    ptr_file = fopen("STUDENT.DAT", "w");
    fclose(ptr_file);

    printf("\nFile STUDENT.DAT has success-\n");
    printf("fully been created and is now\n");
    printf("ready for student/class data. ");

    for (pauser = 0; pauser < 150000000; pauser++)
    { // pause program so user can read message
    }
    }

    void Add()
    {
    char stuName[30] = "\n";
    // int moreG;
    int moreS = 0;
    // int grade;

    FILE *ptr_file;
    ptr_file = fopen("STUDENT.DAT", "a+");
    fseek(ptr_file, 0L, SEEK_SET); // set pointer to file beginning

    while (moreS != 2)
    {
    printf("Enter student's name: ");
    scanf("%s%s", &stuName);
    fflush(stdin);
    fprintf(ptr_file, "%s %sÂ", stuName);

    printf("More students 1=y/2=n ?");
    scanf("%d", &moreS);
    }
    fclose(ptr_file);
    /*
    moreG = 0;
    grade = 0;
    while (moreG != 2);
    {
    grade++;

    printf("Enter the student's grade #%d: ", grade);
    scanf("%s", &string);
    fflush(stdin);
    fprintf(ptr_file, "%s%s%c", string, '\n');

    printf("Enter the code no. for this grade\n", grade);
    printf("(1=project grade, 2=exam grade): ");
    scanf("%s", &string);
    fflush(stdin);
    fprintf(ptr_file, "%s%s%c", string, '\n');

    printf("Enter more grades (1=yes 2=no)?");
    scanf("%d", &moreG);
    fflush(stdin);
    }

    printf("Enter more student records (1=yes 2=no)?");
    scanf("%d", &moreS);
    fflush(stdin);
    */
    }

    void Edit()
    {
    printf("got it");
    system("pause");
    }

    void Delete()
    {
    printf("got it");
    system("pause");
    }

    void DisplayS()
    {
    printf("got it");
    system("pause");
    }

    void DisplayC()
    {
    char string2[30];
    FILE *ptr_file;
    ptr_file = fopen("STUDENT.DAT", "r+");
    char string[30] = "";
    fseek(ptr_file, 0L, SEEK_SET); // set pointer to file beginning

    fscanf(ptr_file, "%s %s", &string2); // read first two strings from file
    printf("%s %s", string2); // display strings
    system("pause");
    fclose(ptr_file);
    }
    Share on Google+

  2. #2
    this is what im trying to do.

    For the final project you are to write the code necessary to convert the grade average program to a file based system. This means that you must have a routine to enter student data into a record in a file, a routine to read the student data from the file into an array, and a routine that will allow a user to enter new records to an existing file. The rest of the program should continue to work as in your previous program. You will have additional output reports (below).

    File Specifications:
    Your data files should be stored on your floppy disk under the name Student.dat. When you read Student.dat in, you should rename the original file Student.bak so you will have the original data in case any data is changed by the program.

    Output:
    1. Student Grade Reports:
    A) The individual student grade report will remain as before.
    B) A list of student total points, and average

    2. Menu:
    Your program should have a menu that will allow the user to select from the following operations:
    1. Create student file
    2. Display student data - individual student and entire list
    3. Update student file - add, edit, and delete records
    The menu should have a 5 space left margin and a 1" top margin.

    3. File Creation and Update Report:
    Clear screen before display
    File Creation Report
    Student.dat


    Records successfully written: xxx
    Created on: creation date
    Storage Location: A:\
    Volume: your name

    For Submission:
    1. Folder
    2. Cover sheet
    3. VTOC of entire program
    4. Use case model for entire program (including all functions)
    5. Print out of source code
    6. Print screens of all output
    7. Disk containing source and exe code
    All documentation must be typed and in presentation form. When you leave this class you should be able to take your folder and show it to a prospective employer as a sample of your work. Prepare it accordingly.

    Presentation:
    You will be asked to give a 10 min max presentation of your program to the class. You will need to be able to demo how a selected part of the program runs, how you broke the problem into functions, and be able to answer questions about your code.
    Share on Google+

  3. #3
    Fastest Thing Alive s0nIc's Avatar
    Join Date
    Sep 2001
    Location
    Sydney
    Posts
    1,584

    Cool

    hmm gimme some time.. ill come back wid an answer soon...

    Share on Google+

  4. #4
    Senior Member
    Join Date
    Nov 2001
    Posts
    472
    Ok, the problem is to read from file and insert into an array? This code reads from a file with words seperated by space.
    Here goes:

    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>

    void readFromFile()
    {
    ifstream inFile; //infile stream
    char inWord;
    String myArray[5000] ; //max words = 5000
    int counter = 0;

    inFile.open("c:\\yourprog\\yourfile.dat") ; //open the file
    while(inFile.get(inWord), !inFile.eof() ) //loop through the file, word by word
    {
    myArray[counter]=inWord; //assign the char to String-array
    counter++;
    }
    inFile.close() ;

    }


    This is not C-code, it's C++. But maybe it will help anyway.
    ---
    proactive
    Share on Google+

  5. #5
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    What compiler are you supposed to use? I can write this prog in Borland or Turbo C but otherwise writing the clear screen routine is a real pain in the A$$.
    I'll post the code soon.
    Share on Google+

  6. #6

    due tomarrow

    well this is due tomarrow if i cant figure this out im screwed.
    Share on Google+

  7. #7
    Senior Member
    Join Date
    Jul 2001
    Posts
    143
    Evilseed:

    Well, being a comp sci major that REALLY hates the fact that most people in my classes couldn't program if their lives depended on it, I won't give you all the code, but I will tell you that you want to use Fread to read in the file and have a while loop that looks a little like this:

    while(input!=eof){
    input.get(blah);
    do your stuff on the characters;
    }

    Good luck on your project...

    -Wizeman
    \"It\'s only arrogrance if you can\'t back it up, otherwise it is confidence.\" - Me
    Share on Google+

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides