File input/output help
-
-
GDay everyone, I need to save received characters into a file and display them to the screen later on in C programming. How to write a character pointed into a file in function A, and display it in a function B? Please help. Thanks in advance
eric
You can do that by many ways. You can use std::fstream , or the MFC's CFile or a direct "fopen" in C. Try searching these keys.
--[:jig:]-- [My Current Status] Link2006 wrote:Let's take it outside of CP Jeremy : Please don't.I would love to see this.I'm making the popcorn already.
-
GDay everyone, I need to save received characters into a file and display them to the screen later on in C programming. How to write a character pointed into a file in function A, and display it in a function B? Please help. Thanks in advance
eric
u can use fopen method to open a file like this
FILE* pFile = fopen("file.txt", "a");
where "file.txt" is the file name and "a" is the mode (Opens for writing at the end of the file (appending)) u can learn more if u look for this function in the MSDN help use fprintf to write to the filefprintf(pFile, "%s\n",szString);
where szString is any Character string !! note: dont forget to close the filefclose(pFile);
-
GDay everyone, I need to save received characters into a file and display them to the screen later on in C programming. How to write a character pointed into a file in function A, and display it in a function B? Please help. Thanks in advance
eric
Thanks very much. Here is the code I've just done, but I can't display the right format Please give me a fix.
#include
void display(){
FILE *file1;
int numbers[30];
/* make sure it is large enough to hold all the data! */
int i,j;
//////////////////////
file1 = fopen("try.txt", "r");if(file1==NULL) {
printf("Error: can't open file.\n");
}
else {
printf("File opened successfully.\n");i = 0 ; while(!feof(file1)) { /\* loop through and store into the array \*/ fscanf(file1, "%s", numbers\[i\]); i++; } printf("Number of character read: %d\\n\\n", i); printf("The message are:\\n"); for(j=0 ; j
eric
-
Thanks very much. Here is the code I've just done, but I can't display the right format Please give me a fix.
#include
void display(){
FILE *file1;
int numbers[30];
/* make sure it is large enough to hold all the data! */
int i,j;
//////////////////////
file1 = fopen("try.txt", "r");if(file1==NULL) {
printf("Error: can't open file.\n");
}
else {
printf("File opened successfully.\n");i = 0 ; while(!feof(file1)) { /\* loop through and store into the array \*/ fscanf(file1, "%s", numbers\[i\]); i++; } printf("Number of character read: %d\\n\\n", i); printf("The message are:\\n"); for(j=0 ; j
eric
eric_tran wrote:
while(!feof(file1)) {
/* loop through and store into the array */
fscanf(file1, "%s", numbers[i]);
i++;
}%s reads strings, you need numbers (cause you've declared numbers to be a pointer to int), try fscanf(file1, "%d", &numbers[1]);
eric_tran wrote:
for(j=0 ; j printf("%s\n", &numbers[j]); }
for(j = 0;j<i;j++)
printf("%d ", numbers[j]); -
eric_tran wrote:
while(!feof(file1)) {
/* loop through and store into the array */
fscanf(file1, "%s", numbers[i]);
i++;
}%s reads strings, you need numbers (cause you've declared numbers to be a pointer to int), try fscanf(file1, "%d", &numbers[1]);
eric_tran wrote:
for(j=0 ; j printf("%s\n", &numbers[j]); }
for(j = 0;j<i;j++)
printf("%d ", numbers[j]); -
That's right. I forgot to change it. I'd actually want to display string only. Thanks
eric
easy way (not best)
#include int ReadAndStore(char lines[1024][1024])
{
int numberOfLinesRead = 0;
int i;//index//try to open the file FILE\* theFile = fopen("somefile", "r"); if(theFile==NULL)//if it fails return -1; //exit this function with -1 //read the file into lines printf("Opened file for reading\\n"); numberOfLinesRead = 0; while(fgets(lines\[numberOfLinesRead\],1023,theFile)&&numberOfLinesRead<1024) { //you can also print your data here printf("%s", lines\[numberOfLinesRead\]); //increment line index; numberOfLinesRead++; } fclose(theFile); //print data to screen return numberOfLinesRead;//job well done
}
int main(int argc, char *argv[])
{
printf("scream if this is to big\n");
char fileContents[1024][1024];//should be enough
printf("i didn't scream\n");
int i;
int result = ReadAndStore(fileContents);
//if read returned 1..we exit
if(result==-1)
{
printf("Error reading file");
return 1;
}FILE \*out = fopen("outfile","w"); if(out==NULL) { printf("Error opening file"); return 1; } //print lines to out for(i = 0;i<result;i++) fputs(fileContents\[i\],out); printf("i've read %d lines\\n",result); //close file fclose(out); return 0;
}