Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

fscanf() problems

SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
Alright, I fixed my window problem. Turns out I forgot to load the common-control library. Now the app is running smoothly except for scanning strings in the file. The cache-file contains lines like "A3270VBKJGIGFCGJL-01=Package.ut2". I want to scan everything before the equal-sign into one buffer, and everything after into another. My current method is "fscanf(pFile, "%s=%s
", pBufferA, pBufferB);", but it scans the entire string into the first buffer, and the second is empty. Any ideas?

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • LundinLundin Posts: 3,711Member
    : Alright, I fixed my window problem. Turns out I forgot to load the common-control library. Now the app is running smoothly except for scanning strings in the file. The cache-file contains lines like "A3270VBKJGIGFCGJL-01=Package.ut2". I want to scan everything before the equal-sign into one buffer, and everything after into another. My current method is "fscanf(pFile, "%s=%s
    ", pBufferA, pBufferB);", but it scans the entire string into the first buffer, and the second is empty. Any ideas?
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
    :

    Either scan char by char and look for '=' (most effective method), or you can read the whole string into a temporary buffer:

    [code]
    char buf[x];
    char* strp;

    fscanf(pFile,"%s",buf);

    strp = strchr(buf,'=');
    strcpy(pBufferB,strp+1);
    *strp='';
    strcpy(pBufferA,buf);
    [/code]
  • pingpongpingpong Posts: 937Member
    Or you can use strtok:
    [code]
    char buffer[] = "A3270VBKJGIGFCGJL-01=Package.ut2";

    // strtok will change the buffer, so save to a temp buffer first
    char* temp = (char*)malloc(strlen(buffer) + 1);
    strcpy(temp, buffer);

    const char* sep = "=";
    char* before = strtok(temp, sep);
    char* after = strtok(NULL, sep);

    printf("original: %s
    before: %s
    After: %s
    ", buffer, before, after);

    free(temp);
    // before and after are pointers into temp, so now they are invalid.
    // if you want to save them somewhere, strcpy them before free(temp)
    [/code]
  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    [b][red]This message was edited by Sephiroth at 2004-8-18 17:29:19[/red][/b][hr]
    Still not working. I'll post my entire reading function and the one that shows the data in a list-view. Maybe the list-view is setup wrong.
    [code]
    void SetupColumns()
    {
    HWND pHandle;
    LV_COLUMN sListInfo;

    pHandle = GetDlgItem(sWinData.ReturnHandle(), IDW_CACHE);
    memset(&sListInfo, 0, sizeof(LV_COLUMN));
    sListInfo.mask = LVCF_FMT | LVCF_TEXT;
    sListInfo.fmt = LVCFMT_LEFT;
    sListInfo.pszText = "Cache ID";
    SendMessage(pHandle, LVM_INSERTCOLUMN, (WPARAM)0, (LPARAM)&sListInfo);

    memset(&sListInfo, 0, sizeof(LV_COLUMN));
    sListInfo.mask = LVCF_FMT | LVCF_TEXT;
    sListInfo.fmt = LVCFMT_LEFT;
    sListInfo.pszText = "Real Name";
    SendMessage(pHandle, LVM_INSERTCOLUMN, (WPARAM)1, (LPARAM)&sListInfo);
    }

    void ShowCache()
    {
    HWND pHandle;
    LV_ITEM sListItem;

    pHandle = GetDlgItem(sWinData.ReturnHandle(), IDW_CACHE);
    SendMessage(pHandle, LVM_DELETEALLITEMS, 0, 0);

    for(int sLoop = 0; ((sLoop < sCount) && (sLoop < 512)); sLoop++)
    {
    memset(&sListItem, 0, sizeof(LV_ITEM));
    sListItem.mask = LVIF_TEXT;
    sListItem.iItem = sLoop;
    sListItem.iSubItem = 0;
    sListItem.pszText = pList[sLoop];
    sListItem.cchTextMax = 512;
    SendMessage(pHandle, LVM_INSERTITEM, 0, (LPARAM)&sListItem);

    memset(&sListItem, 0, sizeof(LV_ITEM));
    sListItem.mask = LVIF_TEXT;
    sListItem.iItem = sLoop;
    sListItem.iSubItem = 1;
    sListItem.pszText = pNames[sLoop];
    sListItem.cchTextMax = 512;
    SendMessage(pHandle, LVM_INSERTITEM, 0, (LPARAM)&sListItem);
    }
    }

    bool ReadCache(char *pPath)
    {
    char pBuffer[MAX_PATH], pBackup[MAX_PATH];
    int sLength, sLoop;
    FILE *pFile;

    if(pPath == NULL)
    {
    MessageBox(sWinData.ReturnHandle(), "No UT path was specified.", "ReadCache() Error", MB_OK | MB_ICONEXCLAMATION);
    return false;
    }

    memset(pBuffer, '', sizeof(pBuffer));
    sprintf(pBuffer, "%s\Cache\Cache.ini", pPath);
    if((pFile = fopen(pBuffer, "rt")) == NULL)
    {
    MessageBox(sWinData.ReturnHandle(), "Your cache appears to be empty.", "ReadCache() Error", MB_OK | MB_ICONEXCLAMATION);
    return false;
    }

    fseek(pFile, 0, SEEK_END);
    sLength = ftell(pFile);
    fseek(pFile, 0, SEEK_SET);
    fscanf(pFile, "%s
    ", pBuffer);

    sLoop = 0;
    sCount = 0;
    while(ftell(pFile) < sLength)
    {
    memset(pBuffer, '', sizeof(pBuffer));
    memset(pBackup, '', sizeof(pBackup));
    memset(pList[sLoop], '', 512);
    memset(pNames[sLoop], '', 512);

    fscanf(pFile, "%s
    ", pBuffer);
    strcpy(pBackup, pBuffer);
    sprintf(pList[sLoop], "%s", strtok(pBuffer, "="));
    sprintf(pNames[sLoop], "%s", strtok(pBuffer, "="));
    sLoop++;
    }

    sCount = (sLoop - 1);
    memset(pBuffer, 0, sizeof(pBuffer));
    fclose(pFile);
    return true;
    }
    [/code]
    Is anything wrong there? The window (list-view) only fills in the first line on the first column, but what is there IS corect.

  • pingpongpingpong Posts: 937Member
    try
    [code]
    sprintf(pNames[sLoop], "%s", strtok([red]NULL[/red], "="));
    [/code]

  • LundinLundin Posts: 3,711Member
    : [b][red]This message was edited by Sephiroth at 2004-8-18 17:29:19[/red][/b][hr]
    : Still not working. I'll post my entire reading function and the one that shows the data in a list-view. Maybe the list-view is setup wrong.
    : [code]
    : void SetupColumns()
    : {
    : HWND pHandle;
    : LV_COLUMN sListInfo;
    :
    : pHandle = GetDlgItem(sWinData.ReturnHandle(), IDW_CACHE);
    : memset(&sListInfo, 0, sizeof(LV_COLUMN));
    : sListInfo.mask = LVCF_FMT | LVCF_TEXT;
    : sListInfo.fmt = LVCFMT_LEFT;
    : sListInfo.pszText = "Cache ID";
    : SendMessage(pHandle, LVM_INSERTCOLUMN, (WPARAM)0, (LPARAM)&sListInfo);
    :
    : memset(&sListInfo, 0, sizeof(LV_COLUMN));
    : sListInfo.mask = LVCF_FMT | LVCF_TEXT;
    : sListInfo.fmt = LVCFMT_LEFT;
    : sListInfo.pszText = "Real Name";
    : SendMessage(pHandle, LVM_INSERTCOLUMN, (WPARAM)1, (LPARAM)&sListInfo);
    : }
    :
    : void ShowCache()
    : {
    : HWND pHandle;
    : LV_ITEM sListItem;
    :
    : pHandle = GetDlgItem(sWinData.ReturnHandle(), IDW_CACHE);
    : SendMessage(pHandle, LVM_DELETEALLITEMS, 0, 0);
    :
    : for(int sLoop = 0; ((sLoop < sCount) && (sLoop < 512)); sLoop++)
    : {
    : memset(&sListItem, 0, sizeof(LV_ITEM));
    : sListItem.mask = LVIF_TEXT;
    : sListItem.iItem = sLoop;
    : sListItem.iSubItem = 0;
    : sListItem.pszText = pList[sLoop];
    : sListItem.cchTextMax = 512;
    : SendMessage(pHandle, LVM_INSERTITEM, 0, (LPARAM)&sListItem);
    :
    : memset(&sListItem, 0, sizeof(LV_ITEM));
    : sListItem.mask = LVIF_TEXT;
    : sListItem.iItem = sLoop;
    : sListItem.iSubItem = 1;
    : sListItem.pszText = pNames[sLoop];
    : sListItem.cchTextMax = 512;
    : SendMessage(pHandle, LVM_INSERTITEM, 0, (LPARAM)&sListItem);
    : }
    : }
    :
    : bool ReadCache(char *pPath)
    : {
    : char pBuffer[MAX_PATH], pBackup[MAX_PATH];
    : int sLength, sLoop;
    : FILE *pFile;
    :
    : if(pPath == NULL)
    : {
    : MessageBox(sWinData.ReturnHandle(), "No UT path was specified.", "ReadCache() Error", MB_OK | MB_ICONEXCLAMATION);
    : return false;
    : }
    :
    : memset(pBuffer, '', sizeof(pBuffer));
    : sprintf(pBuffer, "%s\Cache\Cache.ini", pPath);
    [b][red]
    : if((pFile = fopen(pBuffer, "rt")) == NULL)
    [/b][/red]

    [blue]
    I have never heard of "rt", I think this is a typo.
    [/blue]

    : {
    : MessageBox(sWinData.ReturnHandle(), "Your cache appears to be empty.", "ReadCache() Error", MB_OK | MB_ICONEXCLAMATION);
    : return false;
    : }
    :
    : fseek(pFile, 0, SEEK_END);
    : sLength = ftell(pFile);
    : fseek(pFile, 0, SEEK_SET);
    : fscanf(pFile, "%s
    ", pBuffer);
    :
    : sLoop = 0;
    : sCount = 0;
    : while(ftell(pFile) < sLength)
    : {
    : memset(pBuffer, '', sizeof(pBuffer));
    : memset(pBackup, '', sizeof(pBackup));
    : memset(pList[sLoop], '', 512);
    : memset(pNames[sLoop], '', 512);
    :
    : fscanf(pFile, "%s
    ", pBuffer);
    : strcpy(pBackup, pBuffer);
    : sprintf(pList[sLoop], "%s", strtok(pBuffer, "="));
    : sprintf(pNames[sLoop], "%s", strtok(pBuffer, "="));
    : sLoop++;
    : }
    :
    : sCount = (sLoop - 1);
    : memset(pBuffer, 0, sizeof(pBuffer));
    : fclose(pFile);
    : return true;
    : }
    : [/code]
    : Is anything wrong there? The window (list-view) only fills in the first line on the first column, but what is there IS corect.
    :
    :

  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    [b][red]This message was edited by Sephiroth at 2004-8-21 20:6:31[/red][/b][hr]
    [b][red]This message was edited by Sephiroth at 2004-8-21 19:40:12[/red][/b][hr]
    rt means Read Text. Opens the file for reading only in text-mode.

    pingpong, I tried it with NULL prior to posting, and tried it that way before copying and pasting. Neither seemed to work.

    *UPDATE*

    Upon debugging, it is setting 'pNames[]' to the right string (the actual package name), but it refuses to set that name in the second list. Here's the re-drawing code.
    [code]
    void ShowCache()
    {
    HWND pHandle;
    LV_ITEM sListItem;

    pHandle = GetDlgItem(sWinData.ReturnHandle(), IDW_CACHE);
    SendMessage(pHandle, LVM_DELETEALLITEMS, 0, 0);

    for(int sLoop = 0; ((sLoop < sCount) && (sLoop < 512)); sLoop++)
    {
    memset(&sListItem, 0, sizeof(LV_ITEM));
    sListItem.mask = LVIF_TEXT;
    sListItem.iItem = sLoop;
    sListItem.iSubItem = 0;
    sListItem.pszText = pList[sLoop];
    sListItem.cchTextMax = 512;
    SendMessage(pHandle, LVM_SETITEM, 0, (LPARAM)&sListItem);

    memset(&sListItem, 0, sizeof(LV_ITEM));
    sListItem.mask = LVIF_TEXT;
    sListItem.iItem = sLoop;
    sListItem.iSubItem = 1;
    sListItem.pszText = pNames[sLoop];
    sListItem.cchTextMax = 512;
    SendMessage(pHandle, LVM_SETITEM, 0, (LPARAM)&sListItem);
    }
    }
    [/code]
    Also, I haven't a clue as to why, but when I click the X button in the upper-right or my self-created exit buttons, it won't exit. They just get clicked. Now when I move the window and click either one again, they work! Also, sometimes even after moving the window, the buttons won't work. Any ideas?



  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    Fixed it. Apparently you use LVM_INSERTITEM to insert a new line in every item and sub-item. Then you use LVM_SETITEM to set only the sub-items. It works fine now. Thanks guys!

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • LundinLundin Posts: 3,711Member
    : [b][red]This message was edited by Sephiroth at 2004-8-21 20:6:31[/red][/b][hr]
    : [b][red]This message was edited by Sephiroth at 2004-8-21 19:40:12[/red][/b][hr]
    : rt means Read Text. Opens the file for reading only in text-mode.
    :


    [blue]
    r means read text... or at least I thought so. I'm curious to know where rt comes from: according to ANSI C these are the available modes:

    [italic]
    The argument mode points to a string. If the string is one of the following, the file is
    open in the indicated mode. Otherwise, the behavior is undefined.

    r open text file for reading
    w truncate to zero length or create text file for writing
    a append; open or create text file for writing at end-of-file
    rb open binary file for reading
    wb truncate to zero length or create binary file for writing
    ab append; open or create binary file for writing at end-of-file
    r+ open text file for update (reading and writing)
    w+ truncate to zero length or create text file for update
    a+ append; open or create text file for update, writing at end-of-file

    r+b or rb+ open binary file for update (reading and writing)
    w+b or wb+ truncate to zero length or create binary file for update
    a+b or ab+ append; open or create binary file for update, writing at end-of-file
    [/italic]

    What I think is happening is that you write "rt" and the parser reads "r", and then it finds a char that it doesn't accept, and therefore only uses the first char, "r".

    But it obviously wasn't the cause of your problem... :-)

    [/blue]

    : pingpong, I tried it with NULL prior to posting, and tried it that way before copying and pasting. Neither seemed to work.
    :
    : *UPDATE*
    :
    : Upon debugging, it is setting 'pNames[]' to the right string (the actual package name), but it refuses to set that name in the second list. Here's the re-drawing code.
    : [code]
    : void ShowCache()
    : {
    : HWND pHandle;
    : LV_ITEM sListItem;
    :
    : pHandle = GetDlgItem(sWinData.ReturnHandle(), IDW_CACHE);
    : SendMessage(pHandle, LVM_DELETEALLITEMS, 0, 0);
    :
    : for(int sLoop = 0; ((sLoop < sCount) && (sLoop < 512)); sLoop++)
    : {
    : memset(&sListItem, 0, sizeof(LV_ITEM));
    : sListItem.mask = LVIF_TEXT;
    : sListItem.iItem = sLoop;
    : sListItem.iSubItem = 0;
    : sListItem.pszText = pList[sLoop];
    : sListItem.cchTextMax = 512;
    : SendMessage(pHandle, LVM_SETITEM, 0, (LPARAM)&sListItem);
    :
    : memset(&sListItem, 0, sizeof(LV_ITEM));
    : sListItem.mask = LVIF_TEXT;
    : sListItem.iItem = sLoop;
    : sListItem.iSubItem = 1;
    : sListItem.pszText = pNames[sLoop];
    : sListItem.cchTextMax = 512;
    : SendMessage(pHandle, LVM_SETITEM, 0, (LPARAM)&sListItem);
    : }
    : }
    : [/code]
    : Also, I haven't a clue as to why, but when I click the X button in the upper-right or my self-created exit buttons, it won't exit. They just get clicked. Now when I move the window and click either one again, they work! Also, sometimes even after moving the window, the buttons won't work. Any ideas?
    :
    :
    :
    :



  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    You're right and wrong. If you use just 'r', it normally defaults to text-mode, but I put the 't' for safety. Not a requirement, but good practice IMO.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Sign In or Register to comment.