Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Reading And Writing Files In C

hello world read write file

  • Please log in to reply
40 replies to this topic

#25 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Location:USA
  • Programming Language:C, Java, Python, Bash, Others
  • Learning:Assembly, Scheme

Posted 09 April 2010 - 02:00 PM

It should be pretty simple. Just loop through each student and check to see if the ID matches.
void LetUserSearchForStudent() 
{
// Getting the ID from the user to search for
int id = GetIDFromUser();
int i;
for (i=0;i<10;i++) {
if (id==students[i].ID) {
printf("%s,%s\n", students[i].LastName, students[i].FirstName);
return;
}
printf("Student not found!\n");
}
I just made the function print the last and first name of the student when the ID matches. You can make it do whatever you want.
  • 0

#26 Bouki

Bouki

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 09 April 2010 - 02:26 PM

Thanks alot. This really made me learn quite a few things about indexes and thats where i was mixing up.

In the program i was trying to find a way to update the file by searching for a particular file like u showed me just now. In another function could i call back that function then change an information from the data searched for?
  • 0

#27 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Location:USA
  • Programming Language:C, Java, Python, Bash, Others
  • Learning:Assembly, Scheme

Posted 09 April 2010 - 02:32 PM

Yeah you should be able to do that.
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#28 Bouki

Bouki

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 09 April 2010 - 02:41 PM

ok..ill try it and post it later so u can direct me ok
  • 0

#29 Bouki

Bouki

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 09 April 2010 - 08:46 PM

I tried this function but not gettin what i want. Could u say step by step what i need to do plz

void UpdateStudents(Student students[])
{
char name[10];
int position;
int change;

printf("Enter the First name of the person record you want to edit: ");
scanf("%d", &name);
for (int i=0;i<10;i++)
{
if (name==students[i].FirstName)
{
printf("Enter Index position of desired changes[0-6]:");
fflush(stdin);
scanf("%d", &position);

printf("What do you want to change it to:");
fflush(stdin);
scanf("%d", &change);

Student students[position]=change;
}
}


}

  • 0

#30 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Location:USA
  • Programming Language:C, Java, Python, Bash, Others
  • Learning:Assembly, Scheme

Posted 09 April 2010 - 09:12 PM

scanf("%d", &name);
This won't work because %d tells scanf to get an integer. name is a string, and the proper way to get a string is with fgets:
fgets(name, 10, stdin);
if (name==students[i].FirstName)
In C, you can not compare to strings like this. To compare strings, you need to include the string.h file and use the strcmp function.
if (!strcmp(name, students[i].FirstName))
                printf("Enter Index position of desired changes[0-6]:"); 
fflush(stdin);
scanf("%d", &position);

printf("What do you want to change it to:");
fflush(stdin);
scanf("%d", &change);

Student students[position]=change;
I don't know what you are trying to do here, but it is definitely wrong. What is the user changing exactly? Why does the user need to choose an index position if they already input the first name?
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#31 Bouki

Bouki

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 09 April 2010 - 09:22 PM

What im trying to do is allow the user to change anything he wants from the record. Updating it if he wants to change a name, id number, dob etc...
  • 0

#32 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Location:USA
  • Programming Language:C, Java, Python, Bash, Others
  • Learning:Assembly, Scheme

Posted 09 April 2010 - 09:39 PM

You are going to have to add the other options, but here is a head start:
int choice;
printf("What do you want to change?\n");
printf("1. Change ID number\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Input new ID number: ");
scanf("%d", &students[i].ID);
break;
default:
printf("Bad input\n");
break;
}
If you need to, read up on the C switch statement:
Cprogramming.com Tutorial: Switch-case structure
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#33 Bouki

Bouki

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 09 April 2010 - 09:49 PM

Thank you very much..I am gonna finish up and post it
  • 0

#34 Bouki

Bouki

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 09 April 2010 - 10:13 PM

What do u think abt this code
void UpdateStudents(Student students[])
{
char name[10];
int position;
int change;

printf("Enter the First name of the person record you want to edit: ");
scanf("%d", &name);
for (int i=0;i<10;i++)
{
if (strcmp(name,students[i].FirstName) == 0)
{
printf("Enter Index position of desired changes[0-6]:");
scanf("%d", &position);

printf("What do you want to change it to:");

switch(position)
{

case 0: scanf("%d",change_int);
student[i].id = change_int;
break;

case 1: scanf("%s",change_str);
strcpy(student.LastName,change_str);
break;

case 2: scanf("%s",change_str);
strcpy(student.FirstName,change_str);
break;

case 3: scanf("%d",change_int);
student[i].DateOfBirth = change_int;
break;


default: break;
}

}


  • 0

#35 Bouki

Bouki

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 09 April 2010 - 10:20 PM

How can i write what the user change back to the file "record.txt"

void UpdateStudents(Student students[])
{
int choice;
for (int i=0;i<10;i++)
{

printf("What do you want to change?\n");
printf("1. Change ID number\n 2. Change Last Name\n 3. Change First Name\n 4. Change Date of Birth\n 5. Change Address\n 6. Change Telephone number\n 7. Change Program\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Input new ID number: ");
scanf("%d", &students[i].ID);
break;

case 2:
printf("Input new Last Name: ");
scanf("%d", &students[i].LastName);
break;

case 3:
printf("Input new First Name: ");
scanf("%d", &students[i].FirstName);
break;

case 4:
printf("Input new Date of Birth: ");
scanf("%d", &students[i].DateOfBirth);
break;

case 5:
printf("Input new Address: ");
scanf("%d", &students[i].Address);
break;

case 6:
printf("Input new Telephone Number: ");
scanf("%d", &students[i].TelephoneNumber);
break;

case 7:
printf("Input new Program: ");
scanf("%d", &students[i].ProgramPursued);
break;

default:
printf("Bad input\n");
break;
}

} }

  • 0

#36 alaukik

alaukik

    CC Newcomer

  • Just Joined
  • PipPip
  • 17 posts

Posted 20 June 2010 - 02:31 AM

BUMP

does it work in c++ the same way?
  • 0