C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 18th, 2012, 08:20 AM
aug581 aug581 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 aug581 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 24 sec
Reputation 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();

}

Reply With Quote
  #2  
Old December 18th, 2012, 08:30 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
> 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

Reply With Quote
  #3  
Old December 18th, 2012, 08:37 AM
aug581 aug581 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 aug581 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 24 sec
Reputation 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

Reply With Quote
  #4  
Old December 18th, 2012, 08:41 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
> 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

Reply With Quote
  #5  
Old December 18th, 2012, 08:45 AM
aug581 aug581 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 aug581 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 24 sec
Reputation Power: 0
Quote:
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?

Reply With Quote
  #6  
Old December 18th, 2012, 08:58 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
> 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.

Reply With Quote
  #7  
Old December 18th, 2012, 09:03 AM
aug581 aug581 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 aug581 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 24 sec
Reputation Power: 0
only if i had known that much programming

Reply With Quote
  #8  
Old December 18th, 2012, 09:42 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need help please

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap