Thread: Write to a Text File

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    49

    Question Write to a Text File

    Within this function:

    Code:
    static void horiz_edge(void)
    {
       int y;
       int line_no;
       int current;
       int *pe_ptr;
       int *ne_ptr;
       int *pe_base;
       int *ne_base;
       int pe_cnt[2];
       int ne_cnt[2];
       int n, ndiv5;
    
       ndiv5 = (ia_nx - 4) / 5;
       n = (ndiv5 * 5) - 1;
    
       line_no = 0;
       for (y = dm_ls >> 1; y < ia_ny; y += dm_ls)
       {
          current = line_no & 1;
          pe_base = pe_ptr = &dm_pe[current ? dm_hnx : 0];
          ne_base = ne_ptr = &dm_ne[current ? dm_hnx : 0];
    
          pe_cnt[current] = pe_ptr - pe_base;
          ne_cnt[current] = ne_ptr - ne_base;
       }
    I need to write pe_cnt[current] to a Text file and also pe_base. I've been trying to work with CreateFile() and WriteFile(), but unfortunately, the Text files keep coming up empty. Any help would be greatly appreciated. Thanks!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Use the C standard library first before you move to O/S specific functions. They are possibly easier, and they are more portable.

    Open the file with fopen(), and then write to it with fprintf(). Reading can be done via fgets() later on.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Your problem is probably that you are expecting them to be in the text file as text, but you're not converting them. Text is only useful if it is necessary that people should be able to read and modify it by hand. If you are sure you want them as text, then you can use this (Attention! Windows-based code):
    Code:
    char* buffer=(char*)HeapAlloc(GetProcessHeap());
    if(buffer){
        DWORD written;
        sprintf(buffer,"&#37;d %d",pe_cnt[current],pe_base);
        HANDLE file=CreateFile("data.txt",GENERIC_WRITE,7,0,0,CREATE_ALWAYS,0,0);
        if(file!=INVALID_HANDLE_VALUE){
            WriteFile(file,buffer,strlen(buffer),&written,0);
            CloseHandle(file);
        }
        HeapFree(GetProcessHeap(),0,buffer);
    }
    CRT code:
    Code:
    FILE file=fopen("data.txt","w");
    if(file){
        fprintf(file,"%d %d",pe_cnt[current],pe_base);
        fclose(file);
    }
    Last edited by maxorator; 08-13-2007 at 03:04 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    49

    Smile

    maxorator, thank you for the explanation; it makes a lot more sense now. Worked perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. How to write data in exist text file?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2002, 05:55 AM