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

#13 Guest

Guest

    CC Devotee

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

Posted 13 January 2010 - 11:18 PM

Thank you. :)
I'm glad this has been informative.
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#14 LuiDaHottest

LuiDaHottest

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 14 January 2010 - 08:04 PM

nice nice
  • 0

#15 LuiDaHottest

LuiDaHottest

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 14 January 2010 - 08:04 PM

very nice
  • 0

#16 arkanion

arkanion

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 15 January 2010 - 02:27 PM

thanks
  • 0

#17 se7en

se7en

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 07 March 2010 - 10:09 AM

wow, this site is full of awesome tuts.
Thanks m8.
  • 0

#18 Guest

Guest

    CC Devotee

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

Posted 07 March 2010 - 05:12 PM

You're welcome.
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#19 Bouki

Bouki

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 09 April 2010 - 05:39 AM

How can i store this record into a file call "record.txt"
struct student students[10]={ 
{"123","Hall", "Rayon", "160687", "43_Olympic_Court", "898-0497", "Computer"},
{"234","Spencer", "Sochelle", "220587", "29_Decent_Village", "528-5214", "Business"},
{"345","Dobson", "Dwayne", "890583", "263_Far Park Blvd", "457-2014", "Computer"},
{"456","Clarke", "Dave", "10181", "87_Rasta_Village", "354-5874", "Law"},
{"567","Currie", "Nickeisha", "230491", "26_Waterground_rd", "898-6578", "Nurse"},
{"678","Blackwood", "Mariann", "090490", "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"},
};


I tried this function but it doest seems to work
void LoadStudents(Student students[]) 
{
FILE *fp;
fp=fopen("records.txt","a+");
}

  • 0

#20 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 - 11:17 AM

All that function does is open a file. You are going to have to write the members of the "students" struct to the file using the fprintf function.
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#21 Bouki

Bouki

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 09 April 2010 - 11:49 AM

could it be done like this?

void LoadStudents(Student students[])
{

FILE *fp;
fp=fopen("records.txt","a+");
fprintf(student[], fp);


}

  • 0

#22 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 - 11:55 AM

It won't work like that. You need to follow the syntax of fprintf. Something like this:
int i;
for (i=0;i<10;i++) {
fprintf(fp, "%s", students[i].member);
}
Make sure you replace the .member with the appropriate member of the struct.
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#23 Bouki

Bouki

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 09 April 2010 - 12:09 PM

oh ok...thanks
  • 0

#24 Bouki

Bouki

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 09 April 2010 - 01:37 PM

Could u show me how can i search this record from this function i have created?


#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 ( 1 )
{
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(i = 0; i < 10; ++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(i = 0; i < 10; ++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;
}


Search Function
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();

}

  • 0