Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. File input/output help

File input/output help

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eric_tran
    wrote on last edited by
    #1

    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

    E S E 3 Replies Last reply
    0
    • E eric_tran

      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

      E Offline
      E Offline
      Eytukan
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • E eric_tran

        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

        S Offline
        S Offline
        singersinger
        wrote on last edited by
        #3

        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 file fprintf(pFile, "%s\n",szString); where szString is any Character string !! note: dont forget to close the file fclose(pFile);

        1 Reply Last reply
        0
        • E eric_tran

          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

          E Offline
          E Offline
          eric_tran
          wrote on last edited by
          #4

          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

          E 1 Reply Last reply
          0
          • E eric_tran

            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

            E Offline
            E Offline
            eusto
            wrote on last edited by
            #5

            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]);

            E 1 Reply Last reply
            0
            • E eusto

              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]);

              E Offline
              E Offline
              eric_tran
              wrote on last edited by
              #6

              That's right. I forgot to change it. I'd actually want to display string only. Thanks

              eric

              E 1 Reply Last reply
              0
              • E eric_tran

                That's right. I forgot to change it. I'd actually want to display string only. Thanks

                eric

                E Offline
                E Offline
                eusto
                wrote on last edited by
                #7

                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;
                

                }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups