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. doubt in CFile

doubt in CFile

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialhelpquestion
12 Posts 8 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.
  • R Rakesh5

    hi all, i am creating an application in which i want to write the content into an excel. i have two doubts here. doubt #1 i am actually using CFile.write function to write data into an excel. but,if i happen to write second time into the excel, it is overwriting the first item instead of appending it to the excel as second item. what should we do for this? myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL); myfile.Write(sUserID,sUserID.GetLength()); doubt #2 while writing in an excel, i have got three parameters to be written. (name,age and sex). all these three parameters are written in one cell. how to write them in a separate cells? Thanks for your help in advance.. Rakesh.

    J Offline
    J Offline
    Jonathan Davies
    wrote on last edited by
    #2

    Sounds like you need to move the file pointer on after the write. There's probably a method of CFile to do this, just pass it the GetLength() value. If you don't the pointer stays pointing to the 0 position in the file so the next write will overwrite the previous.

    R L 2 Replies Last reply
    0
    • R Rakesh5

      hi all, i am creating an application in which i want to write the content into an excel. i have two doubts here. doubt #1 i am actually using CFile.write function to write data into an excel. but,if i happen to write second time into the excel, it is overwriting the first item instead of appending it to the excel as second item. what should we do for this? myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL); myfile.Write(sUserID,sUserID.GetLength()); doubt #2 while writing in an excel, i have got three parameters to be written. (name,age and sex). all these three parameters are written in one cell. how to write them in a separate cells? Thanks for your help in advance.. Rakesh.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      Rakesh5 wrote:

      CFile::modeCreate

      You are recreating the file each time with new content using that flag.

      Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns Help humanity, join the CodeProject grid computing team here

      1 Reply Last reply
      0
      • R Rakesh5

        hi all, i am creating an application in which i want to write the content into an excel. i have two doubts here. doubt #1 i am actually using CFile.write function to write data into an excel. but,if i happen to write second time into the excel, it is overwriting the first item instead of appending it to the excel as second item. what should we do for this? myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL); myfile.Write(sUserID,sUserID.GetLength()); doubt #2 while writing in an excel, i have got three parameters to be written. (name,age and sex). all these three parameters are written in one cell. how to write them in a separate cells? Thanks for your help in advance.. Rakesh.

        K Offline
        K Offline
        krmed
        wrote on last edited by
        #4

        After you open the file, use the SeekToEnd function:

        myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL);
        myfile.SeekToEnd();
        myfile.Write(sUserID,sUserID.GetLength());

        I can't say that I've ever seen anyone use CFile to write to an excel file, so I'm not sure how you're accomplishing that. Most people would write the data to a .csv file, which can then be opened in excel. Each cell data on the same line would be separated by a comma, with a CR/LF pair at the end of that line. I don't think you're actually creating a true .xls file - it's most likely just a text file, but with the .xls extension, excel may be able to open it. Be aware that just because you name a file with .xls as an extension, it's not really an Excel file. Hope that helps.

        Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

        R 1 Reply Last reply
        0
        • R Rakesh5

          hi all, i am creating an application in which i want to write the content into an excel. i have two doubts here. doubt #1 i am actually using CFile.write function to write data into an excel. but,if i happen to write second time into the excel, it is overwriting the first item instead of appending it to the excel as second item. what should we do for this? myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL); myfile.Write(sUserID,sUserID.GetLength()); doubt #2 while writing in an excel, i have got three parameters to be written. (name,age and sex). all these three parameters are written in one cell. how to write them in a separate cells? Thanks for your help in advance.. Rakesh.

          2 Offline
          2 Offline
          224917
          wrote on last edited by
          #5

          CFile is not the right object to work with excel. Presently what you are doing is, creating a text file with name "example.xls" and writing/overwriting its data. Maybe using "modeAppend" may help you to get away with the overwriting issue. But still may not serve your purpose. To modify an "real" excel file, or creating a new excel you need to use Excel . Microsoft provides a way to start excel.exe invisible through your program and then open an existing/create_new excel file and make changes to it. This is called Excel Automation/Office Automation. http://support.microsoft.com/kb/196776[^]

          Suhredayan

          1 Reply Last reply
          0
          • R Rakesh5

            hi all, i am creating an application in which i want to write the content into an excel. i have two doubts here. doubt #1 i am actually using CFile.write function to write data into an excel. but,if i happen to write second time into the excel, it is overwriting the first item instead of appending it to the excel as second item. what should we do for this? myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL); myfile.Write(sUserID,sUserID.GetLength()); doubt #2 while writing in an excel, i have got three parameters to be written. (name,age and sex). all these three parameters are written in one cell. how to write them in a separate cells? Thanks for your help in advance.. Rakesh.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #6

            In addition to the Excel Automation suggestions, you could also try using CRecordset with Excel's ODBC interface.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            M 1 Reply Last reply
            0
            • K krmed

              After you open the file, use the SeekToEnd function:

              myfile.Open("example.xls",CFile::modeCreate|CFile::modeWrite, NULL);
              myfile.SeekToEnd();
              myfile.Write(sUserID,sUserID.GetLength());

              I can't say that I've ever seen anyone use CFile to write to an excel file, so I'm not sure how you're accomplishing that. Most people would write the data to a .csv file, which can then be opened in excel. Each cell data on the same line would be separated by a comma, with a CR/LF pair at the end of that line. I don't think you're actually creating a true .xls file - it's most likely just a text file, but with the .xls extension, excel may be able to open it. Be aware that just because you name a file with .xls as an extension, it's not really an Excel file. Hope that helps.

              Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

              R Offline
              R Offline
              Rakesh5
              wrote on last edited by
              #7

              hi, thanks alot for your info.. i tried using seektoend function. but it is still overwriting the value.. can u please help me to solve this problem? and i don understand how to write each value in each cell? can u explain me little more? thanks, rakesh.

              1 Reply Last reply
              0
              • J Jonathan Davies

                Sounds like you need to move the file pointer on after the write. There's probably a method of CFile to do this, just pass it the GetLength() value. If you don't the pointer stays pointing to the 0 position in the file so the next write will overwrite the previous.

                R Offline
                R Offline
                Rakesh5
                wrote on last edited by
                #8

                hi jonathan, thanks alot for your reply.. can u please teach me how to pass the pointer using the getlength method? thanks once again for patiently replying.. rakesh.

                J 1 Reply Last reply
                0
                • R Rakesh5

                  hi jonathan, thanks alot for your reply.. can u please teach me how to pass the pointer using the getlength method? thanks once again for patiently replying.. rakesh.

                  J Offline
                  J Offline
                  Jonathan Davies
                  wrote on last edited by
                  #9

                  Have a look at http://msdn.microsoft.com/en-us/library/8c5ccz0x(VS.80).aspx[^] where there's an example, remember it moves the pointer a number of bytes.

                  1 Reply Last reply
                  0
                  • J Jonathan Davies

                    Sounds like you need to move the file pointer on after the write. There's probably a method of CFile to do this, just pass it the GetLength() value. If you don't the pointer stays pointing to the 0 position in the file so the next write will overwrite the previous.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    The file pointer gets updated automatically on a read or write operation. The next write to the same file will be positioned after the first write's data.

                    1 Reply Last reply
                    0
                    • D David Crow

                      In addition to the Excel Automation suggestions, you could also try using CRecordset with Excel's ODBC interface.

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      M Offline
                      M Offline
                      Mattias G
                      wrote on last edited by
                      #11

                      I you'd prefer the text format over the CRecordset/Automation approaches, you could use a tab character ("\t") or a comma to separate the cells, and a newline to separate the rows. If you only use the CFile::modeCreate flag when opening the file, the file gets truncated to zero length, effectively overwriting your data. Have a look at CFile::modeNoTruncate. Something like this:

                      CFile theFile;
                      theFile.Open(_T("example.txt"), CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite, NULL);
                      CString strOut = _T("Row1Col1\tRow1Col2\tRow1Col3\r\nRow2Col1\tRow2Col2\tRow2Col3");
                      theFile.Write(strOut, strOut.GetLength() * sizeof(TCHAR));

                      Note how the file name extension is "txt", you can still open it in Microsoft Excel by right-clicking it. Or you could keep the "xls" extension and keep pretending it's a real binary Excel file. No user will ever notice :-)

                      P 1 Reply Last reply
                      0
                      • M Mattias G

                        I you'd prefer the text format over the CRecordset/Automation approaches, you could use a tab character ("\t") or a comma to separate the cells, and a newline to separate the rows. If you only use the CFile::modeCreate flag when opening the file, the file gets truncated to zero length, effectively overwriting your data. Have a look at CFile::modeNoTruncate. Something like this:

                        CFile theFile;
                        theFile.Open(_T("example.txt"), CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite, NULL);
                        CString strOut = _T("Row1Col1\tRow1Col2\tRow1Col3\r\nRow2Col1\tRow2Col2\tRow2Col3");
                        theFile.Write(strOut, strOut.GetLength() * sizeof(TCHAR));

                        Note how the file name extension is "txt", you can still open it in Microsoft Excel by right-clicking it. Or you could keep the "xls" extension and keep pretending it's a real binary Excel file. No user will ever notice :-)

                        P Offline
                        P Offline
                        Paulraj G
                        wrote on last edited by
                        #12

                        1. FILE * pFile; pFile = fopen ("C:file.csv","a"); if (pFile!=NULL) { CString x2 = "CString1"+","+"CString2"+"\n"; fwrite(x2, x2.GetLength(), 1, pFile); fclose (pFile); } 2. Use ,(comma) operator for seperate the objects. use .csv file. it will open in excel.

                        G.Paulraj

                        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