#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Need help please


    i am pasting the code below..all i want to know why is there two dimentional array used in fileread function in char name[100][50] ?



    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    void filewrite();
    void fileread();

    int main()
    {
    int c;

    while(c!=3)
    {
    system("cls");
    // clrscr();
    printf("1 TO ENTER STUDENT INFORMATION.\n");
    printf("2 TO SEE ALL THE STUDENT INFORMATION\n");
    printf("3 TO EXIT\n\n");
    printf("Enter your choice: ");
    scanf("%d",&c);
    // clrscr();
    switch(c)
    {
    case 1:
    filewrite();
    break;
    case 2:
    fileread();
    break;
    case 3:
    break;
    default:
    break;
    }
    break;

    getch();
    return 0;
    }
    }


    void filewrite()
    {
    int roll,ch,mark;
    char nam[100];
    FILE *fp;
    // clrscr();
    fp = fopen("student.txt","a");
    printf("ENTER ROLL NUMBER, NAME , MARKS \n");
    ch =1;
    while(ch)
    {
    scanf("%d%s%d",&roll,&nam,&mark);
    //fprintf(fp,"ROLLNO. NAME MARK\n");
    fprintf(fp," %d %s %d\n",roll,nam,mark);

    printf("\n\n press 1 to enter more marks,0 to return to main menu");
    scanf("%d",&ch);



    }


    fclose(fp) ;
    system("pause");
    main();
    }


    void fileread()
    {
    int marks[100],rollno[100],i;

    char name[100][50];
    FILE *fp;


    // clrscr();
    system("cls");
    fp = fopen("student.txt","r");
    i=0;
    printf("ROLLNO NAME MARK\n");
    while(!feof(fp))
    {
    fscanf(fp,"%d %s %d\n",&rollno[i],&name[i],&marks[i]);
    printf(" %d %s %d\n\n",rollno[i],name[i],marks[i]);
    i=i+1;
    }
    fclose(fp);

    //printf("\n\n\nPRESS ANY KEY");

    system("pause");
    main();
    getch();

    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > all i want to know why is there two dimentional array used in fileread function in char name[100][50] ?
    A phrase which immediately screams "I didn't write it".

    This particular disaster area of code is all over the place - for example

    Do you understand this?
    Code:
    int age;
    char name[100];
    scanf("%s %d", name, &age );  // read a name and age
    Compare it with
    Code:
    int ages[10];
    char names[10][100];
    scanf("%s %d", names[i], &ages[i] );  // read a name and age
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    yes i do undertsand this..i even tried putting one dimentional array e-g char name[100]. but then it doesnt read the values from the file but as soon as i make it two dimentional array that is char name [100][50] it works fine..all i want to know why [100] and [50] ?
    as in the FILEWRITE function ,it uses only one dimentional array that is char nam[50].

    need help badly
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > all i want to know why [100] and [50] ?
    Try and figure out which number
    - which number represents the number of names to be stored
    - which number represents the maximum number of characters in each name
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by salem
    > all i want to know why [100] and [50] ?
    Try and figure out which number
    - which number represents the number of names to be stored
    - which number represents the maximum number of characters in each name

    [100] represents the number of names and [50] represents number of characters in each name.
    how its read through :
    fscanf(fp,"%d %s %d\n",&rollno[i],&name[i],&marks[i]);
    printf(" %d %s %d\n\n",rollno[i],name[i],marks[i]);


    but in FILEWRITE() its char nam[50]..whys this one D?
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > but in FILEWRITE() its char nam[50]..whys this one D?
    For the same reason that roll and mark are integers, and not arrays of integers.

    Try writing your own code for a change, then you would know line-by-line what each line was for, because you will have had to think about it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    only if i had known that much programming :(
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Originally Posted by aug581
    only if i had known that much programming :(
    Well what were you doing on the course for these past few months, if not learning how to program?

    In the same way that you can't become a chef simply by reading cookery books and eating in restaurants, you need to actually get down to it and do some actual work yourself. You can't just phone it in!

    What you ask should be pretty self-evident to anyone at the end of their first semester programming intro class.

    But if you're just another one of the endless stream of wannabes who seem to think they can roll through their course with just google searches and copy/paste, then you've only got yourself to blame.

    Sooner or later, your luck will run out, the homework won't be on google, and you'll run out of forums to leech off.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo