| | |
write to text file using fprintf()
![]() |
•
•
Join Date: Mar 2006
Posts: 12
Reputation:
Solved Threads: 0
I'm having problems here with this function i'm making, i need to make function that will write to a text file. This function has to ask the user for the file name and check if it exists. If this is all good then it will write the string in the buffer to the text file. The string is no bigger than 60chars.
so far i have started it but i am having diffuculty in understanding it, my text book isn't the best help either. Below is my function so far
void write_string(File *fp, char *s)
{
File *ptr;
int i;
ptr == fopen(fp, "w");
if (ptr == NULL)
}
My problem is what is fp, its coming from the main but what is it exactly?
Here is my code for the whole program please view if it makes more sense..Please i need some help
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>
//void instruct(void);
//void classify(char cInput[], float fstats[], int i, int iOdd, int iEven, int iUpper,
// int iLower, int iOaverage, int iEaverage);
void instruct(char *cInput);
void summary(float fstats[], int i, int iOdd, int iEven, int iUpper,
int iLower, int iOaverage, int iEaverage);
//int count(char cChoice, char cInput[]);
int search(char cGet, char cInput[]);
void menu(void);
int modify ( const char x, const char y, char *s);
void classify(char *cInput, float *fstats);
int count(char cChoice, char *cInput);
void write_string(File *fp, char *s)
void main(void)
{
int iOdd=1, iEven=2;
int i=0, iCheck=0, j, iChange = 0;
int iUpper=3, iLower=4;
int iOaverage =5, iEaverage=6;
char cInput [61] = {"0"};
float fstats[7]={0,0,0,0,0,0,0};
char cPick , cChoice, cGet, cReplace;
cInput[i]='\0';
menu();
cPick = getche();
for (;cPick != '9'; )
{
switch(cPick)
{
case '1': system("cls");
cInput[i]='\0';
for (j=0; j<=6; j++)
fstats[j]=0;
instruct(cInput);
//classify(cInput,fstats,i,iOdd,iEven,iUpper,iLower,iOaverage,iEaverage);
classify(cInput,fstats);
break;
case '2': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
for (j=0; j<iCheck;j++)
printf("%c", cInput[j]);
printf("\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");
//cin.clear();
//cin.ignore(80,'\n');
//cin.get();
system("pause");
break;
case '5': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
printf("Please input the character to search:");
cGet = getche();
if (search(cGet, cInput) > 0)
printf("\nthe character occurs first at the index %d\n",search(cGet,cInput));
else
printf("\nthere are no occurences of that character\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");
//cin.get();
system("pause");
break;
case '6': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
printf("please input character:");
cChoice = getche();
if (count(cChoice,cInput) >0)
printf("\nThere are %d occurences of the character %c\n", count(cChoice, cInput), cChoice);
else
printf("\nthere are no occurences of that character\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");
//cin.get();
system("pause");
break;
case '8': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
summary(fstats,i,iOdd,iEven,iUpper,iLower,iOaverage,iEaverage);
}
else
printf("\nThere is no string in the memory please input a string first\n");
//cin.get();
system("pause");
break;
case '7': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
printf("character to be replaced:");
cChoice = getche();
if (count(cChoice,cInput) >0)
{
printf("\nWith:");
cReplace = getche();
iChange = modify(cChoice,cReplace,cInput);
printf("\nThere have been %d changes made to the string\n", iChange);
classify(cInput, fstats);
}
else
printf("\nthere are no occurences of that character\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");
system("pause");
break;
}
system("cls");
menu();
cPick = getche();
}
printf("\n\n");
}
void instruct(char *cInput)
{
printf("To terminate the program, strike the spacebar or Enter.\n\n\n");
printf("Next key>");
int j =0;
char cTemp;
cTemp = getche();
for(;((cTemp != 32) && (cTemp!= 13)); ) //ascii 32 for blank and 13 for carriage return
{
if (j <=59)
{
if ((cTemp != 8) && (cTemp != 9))
{
*cInput = cTemp;
*(cInput + 1) = '\0';
//fstats[i]++;
j++;
cInput = cInput + 1;
}
cTemp = getche();
}
else
{
printf("\nYou cannot enter more than 60 characters, the first 60 characters will remain\n");
//printf("hit enter to continue");
//cin.get();
system("pause");
break;
}
}
}
void classify(char *cInput ,float *fstats)
{
int j;
int iResult =0;
// int i;
float * pstatbeg;
char * pInputbeg;
pInputbeg = cInput;
pstatbeg = fstats;//char cTemp;
//cTemp = getche();
for (j=0; j<=6; j++)
fstats[j]=0;
//for(;((cTemp != 32) && (cTemp!= 13)); ) //ascii 32 for blank and 13 for carriage return
for ( ; *cInput !='\0'; cInput = cInput + 1)
{
//if ( j<=59)
// {
if (isalpha(*cInput) != 0)
{
if (isupper(*cInput) !=0)
fstats [3]++;
else
fstats[4]++;
}
if(isdigit(*cInput) !=0)
{
if( (*cInput%2) == 0)
{
fstats[2]++;
iResult = *cInput - '0';
*(fstats +6) = (*(fstats + 6) + iResult);
}
else
{
fstats[1]++;
iResult = *cInput - '0';
*(fstats + 5) = (*(fstats + 5) + iResult);
}
}
fstats[0]++;
fstats = pstatbeg;
}
// if ((cTemp != 8) && (cTemp != 9))
// {
// cInput[j]= cTemp;
// cInput[j+1] = '\0';
// fstats[i]++;
// j++;
// }
// cTemp = getche();
// }
// else
// {
// printf("\nYou cannot enter more than 60 characters, the first 60 characters will remain\n");
//printf("hit enter to continue");
//cin.get();
// system("pause");
// break;
// }
//}// for loop
if (*(fstats +2) ==0)
*(fstats +6) = 0;
else
*(fstats + 6) = *(fstats+6)/(*(fstats +2));
if (*(fstats + 1) ==0)
*(fstats + 5) =0;
else
*(fstats + 5) = *(fstats + 5) /(*(fstats +1));
}
void summary(float fstats[], int i, int iOdd, int iEven, int iUpper,
int iLower, int iOaverage, int iEaverage)
{
system("cls");
printf("Thank you! Your summary:\n\n");
printf("Total number of keystrokes entered:%.f\n", fstats[i]);
printf("Upper case alphabetic characters:%.f\n", fstats[iUpper]);
printf("Lower case alphabetic characters:%.f\n", fstats[iLower]);
printf("Even digits:%.f\n", fstats[iEven]);
printf("Odd digits:%.f\n", fstats[iOdd]);
printf("Average value of the even digits entered:%.2f\n", fstats[iEaverage]);
printf("Average value of the odd digits entered:%.2f\n", fstats[iOaverage]);
}
int count(char cChoice, char *cInput)
{
int i, iNum, iTotal =0;
iNum = strlen(cInput);
for ( i=0; i<iNum; i++, cInput = cInput + 1)
{
if (cChoice == *cInput)
iTotal++;
}
return iTotal;
}
int search(char cGet, char *cInput)
{
int i, iIndex=0, iNum=0;
iNum = strlen(cInput);
for (i=0; i < iNum; i++)
{
if (cGet == cInput[i])
{
iIndex = i;
iIndex++;
break;
}
}
return iIndex;
}
void menu(void)
{
printf("1. Enter a string of characters\n");
printf("2. Display the current string\n");
printf("3. Write current string to file\n");
printf("4. Read a string from a file\n");
printf("5. Search for a character\n");
printf("6. Count occurences of a specific character\n");
printf("7. Replace all occurences\n");
printf("8. Produce a summary report on the current string\n");
printf("9. Exit\n\n");
}
int modify ( const char x, const char y, char *s)
{
int iNum, j, i=0;
iNum = strlen(s);
for ( j=0; j < iNum; j++, s= s+1)
{
if (x == *s)
{
*s = y;
i++;
}
}
return i;
}
void write_string(File *fp, char *s)
{
File *ptr;
int i;
ptr == fopen(fp, "w");
if (ptr == NULL)
}
so far i have started it but i am having diffuculty in understanding it, my text book isn't the best help either. Below is my function so far
void write_string(File *fp, char *s)
{
File *ptr;
int i;
ptr == fopen(fp, "w");
if (ptr == NULL)
}
My problem is what is fp, its coming from the main but what is it exactly?
Here is my code for the whole program please view if it makes more sense..Please i need some help
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>
//void instruct(void);
//void classify(char cInput[], float fstats[], int i, int iOdd, int iEven, int iUpper,
// int iLower, int iOaverage, int iEaverage);
void instruct(char *cInput);
void summary(float fstats[], int i, int iOdd, int iEven, int iUpper,
int iLower, int iOaverage, int iEaverage);
//int count(char cChoice, char cInput[]);
int search(char cGet, char cInput[]);
void menu(void);
int modify ( const char x, const char y, char *s);
void classify(char *cInput, float *fstats);
int count(char cChoice, char *cInput);
void write_string(File *fp, char *s)
void main(void)
{
int iOdd=1, iEven=2;
int i=0, iCheck=0, j, iChange = 0;
int iUpper=3, iLower=4;
int iOaverage =5, iEaverage=6;
char cInput [61] = {"0"};
float fstats[7]={0,0,0,0,0,0,0};
char cPick , cChoice, cGet, cReplace;
cInput[i]='\0';
menu();
cPick = getche();
for (;cPick != '9'; )
{
switch(cPick)
{
case '1': system("cls");
cInput[i]='\0';
for (j=0; j<=6; j++)
fstats[j]=0;
instruct(cInput);
//classify(cInput,fstats,i,iOdd,iEven,iUpper,iLower,iOaverage,iEaverage);
classify(cInput,fstats);
break;
case '2': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
for (j=0; j<iCheck;j++)
printf("%c", cInput[j]);
printf("\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");
//cin.clear();
//cin.ignore(80,'\n');
//cin.get();
system("pause");
break;
case '5': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
printf("Please input the character to search:");
cGet = getche();
if (search(cGet, cInput) > 0)
printf("\nthe character occurs first at the index %d\n",search(cGet,cInput));
else
printf("\nthere are no occurences of that character\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");
//cin.get();
system("pause");
break;
case '6': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
printf("please input character:");
cChoice = getche();
if (count(cChoice,cInput) >0)
printf("\nThere are %d occurences of the character %c\n", count(cChoice, cInput), cChoice);
else
printf("\nthere are no occurences of that character\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");
//cin.get();
system("pause");
break;
case '8': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
summary(fstats,i,iOdd,iEven,iUpper,iLower,iOaverage,iEaverage);
}
else
printf("\nThere is no string in the memory please input a string first\n");
//cin.get();
system("pause");
break;
case '7': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
printf("character to be replaced:");
cChoice = getche();
if (count(cChoice,cInput) >0)
{
printf("\nWith:");
cReplace = getche();
iChange = modify(cChoice,cReplace,cInput);
printf("\nThere have been %d changes made to the string\n", iChange);
classify(cInput, fstats);
}
else
printf("\nthere are no occurences of that character\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");
system("pause");
break;
}
system("cls");
menu();
cPick = getche();
}
printf("\n\n");
}
void instruct(char *cInput)
{
printf("To terminate the program, strike the spacebar or Enter.\n\n\n");
printf("Next key>");
int j =0;
char cTemp;
cTemp = getche();
for(;((cTemp != 32) && (cTemp!= 13)); ) //ascii 32 for blank and 13 for carriage return
{
if (j <=59)
{
if ((cTemp != 8) && (cTemp != 9))
{
*cInput = cTemp;
*(cInput + 1) = '\0';
//fstats[i]++;
j++;
cInput = cInput + 1;
}
cTemp = getche();
}
else
{
printf("\nYou cannot enter more than 60 characters, the first 60 characters will remain\n");
//printf("hit enter to continue");
//cin.get();
system("pause");
break;
}
}
}
void classify(char *cInput ,float *fstats)
{
int j;
int iResult =0;
// int i;
float * pstatbeg;
char * pInputbeg;
pInputbeg = cInput;
pstatbeg = fstats;//char cTemp;
//cTemp = getche();
for (j=0; j<=6; j++)
fstats[j]=0;
//for(;((cTemp != 32) && (cTemp!= 13)); ) //ascii 32 for blank and 13 for carriage return
for ( ; *cInput !='\0'; cInput = cInput + 1)
{
//if ( j<=59)
// {
if (isalpha(*cInput) != 0)
{
if (isupper(*cInput) !=0)
fstats [3]++;
else
fstats[4]++;
}
if(isdigit(*cInput) !=0)
{
if( (*cInput%2) == 0)
{
fstats[2]++;
iResult = *cInput - '0';
*(fstats +6) = (*(fstats + 6) + iResult);
}
else
{
fstats[1]++;
iResult = *cInput - '0';
*(fstats + 5) = (*(fstats + 5) + iResult);
}
}
fstats[0]++;
fstats = pstatbeg;
}
// if ((cTemp != 8) && (cTemp != 9))
// {
// cInput[j]= cTemp;
// cInput[j+1] = '\0';
// fstats[i]++;
// j++;
// }
// cTemp = getche();
// }
// else
// {
// printf("\nYou cannot enter more than 60 characters, the first 60 characters will remain\n");
//printf("hit enter to continue");
//cin.get();
// system("pause");
// break;
// }
//}// for loop
if (*(fstats +2) ==0)
*(fstats +6) = 0;
else
*(fstats + 6) = *(fstats+6)/(*(fstats +2));
if (*(fstats + 1) ==0)
*(fstats + 5) =0;
else
*(fstats + 5) = *(fstats + 5) /(*(fstats +1));
}
void summary(float fstats[], int i, int iOdd, int iEven, int iUpper,
int iLower, int iOaverage, int iEaverage)
{
system("cls");
printf("Thank you! Your summary:\n\n");
printf("Total number of keystrokes entered:%.f\n", fstats[i]);
printf("Upper case alphabetic characters:%.f\n", fstats[iUpper]);
printf("Lower case alphabetic characters:%.f\n", fstats[iLower]);
printf("Even digits:%.f\n", fstats[iEven]);
printf("Odd digits:%.f\n", fstats[iOdd]);
printf("Average value of the even digits entered:%.2f\n", fstats[iEaverage]);
printf("Average value of the odd digits entered:%.2f\n", fstats[iOaverage]);
}
int count(char cChoice, char *cInput)
{
int i, iNum, iTotal =0;
iNum = strlen(cInput);
for ( i=0; i<iNum; i++, cInput = cInput + 1)
{
if (cChoice == *cInput)
iTotal++;
}
return iTotal;
}
int search(char cGet, char *cInput)
{
int i, iIndex=0, iNum=0;
iNum = strlen(cInput);
for (i=0; i < iNum; i++)
{
if (cGet == cInput[i])
{
iIndex = i;
iIndex++;
break;
}
}
return iIndex;
}
void menu(void)
{
printf("1. Enter a string of characters\n");
printf("2. Display the current string\n");
printf("3. Write current string to file\n");
printf("4. Read a string from a file\n");
printf("5. Search for a character\n");
printf("6. Count occurences of a specific character\n");
printf("7. Replace all occurences\n");
printf("8. Produce a summary report on the current string\n");
printf("9. Exit\n\n");
}
int modify ( const char x, const char y, char *s)
{
int iNum, j, i=0;
iNum = strlen(s);
for ( j=0; j < iNum; j++, s= s+1)
{
if (x == *s)
{
*s = y;
i++;
}
}
return i;
}
void write_string(File *fp, char *s)
{
File *ptr;
int i;
ptr == fopen(fp, "w");
if (ptr == NULL)
}
When you post C/C++ code, put the code inside tags like this,
[code]
#include <iostream>
int main()
{
[/code]
and it will appear like this.
[code]
#include <iostream>
int main()
{
std::cout << "Hello World\n";}
return 0;
[/code]
and it will appear like this.
C Syntax (Toggle Plain Text)
#include <iostream> int main() { std::cout << "Hello World\n"; return 0; }
![]() |
Similar Threads
- Cant readline in text file.... URGENT help~ (VB.NET)
- writing students data to a text file (Assembly)
- Text File (Python)
- read from database and writing the contents into a text file (C)
- Output in Text file-How to apply fprintf()? (C)
- Output in the text file (C)
- Read and write to an ASCII Text file (Java)
Other Threads in the C Forum
- Previous Thread: passing values from array to array
- Next Thread: Clearing input
| Thread Tools | Search this Thread |
#include * ansi api array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send sequential shape single socket socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






