Hi,

I'll preface this by saying I am about as beginner as it gets (the "installed compiler on saturday, open first book saturday night" kind of beginner)

Basically, I have a small program that reads the current directory for any file with ".txt" and then opens and scans those files for two words and prints any string that contains those words. For ascii text documents it works a charm but for anything UTF16-32 it parses up empty.. I'm gathering from reading that I somehow need to convert my code into wide characters (or at least the part that reads the .txt files).

Does anyone happen to know a simple way to do this without re-writing the whole thing? Code is as such:

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <conio.h>
int main(void)
{
  DIR           *dirp;
  struct dirent *dir;
  dirp = opendir(".");
  if (dirp)
  {
    while ((dir = readdir(dirp)) != NULL) {
        char *n;

        n = strstr(dir->d_name, ".txt");
        if (n != NULL) {
        printf("%s\n", dir->d_name);

            FILE* file = fopen(dir->d_name , "r");
            char* p = 0;
            char* q = 0;
            char  string [256] = {0};

            while(fgets( string, sizeof string, file ) != NULL) {
                p = strstr(string, "error");
                q = strstr (string, "Fatal");
                if (p != NULL ||
                    q != NULL) {
                    fputs (string, stdout);
                }
            }
        }

    }
    closedir(dirp);
  }
printf("Press any key to continue.\n");
getch();
return (0);
}

Any help or advice/corrections are greatly appreciated,
Thanks!

Recommended Answers

All 4 Replies

Let me start by asking you if you're trying to create a program in C or C++?

Hi Nick,

Thanks for the response, to be honest I've saved the file as a .cpp and am not against using C++ syntax, it just happens that it is in mostly C at the moment. I'm not being picky about it, I just want it to serve it's function for now hah :)

Thought I would add my attempt at the complete widechar conversion:

#include <iostream>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>
#include <conio.h>
using namespace std;

int main(void)
{
  DIR           *dirp;
  struct dirent *dir;
  dirp = opendir(".");
  if (dirp)
  {
    while ((dir = readdir(dirp)) != NULL) {
        char *n;

        n = strstr(dir->d_name, ".txt");
        if (n != NULL) {
        printf("%s\n", dir->d_name);

           FILE* file = fopen(dir->d_name , "rb");
            wchar_t* p = 0;
            wchar_t* q = 0;
            wchar_t  wides [260] = {0};

                string search = "p || q";
                const size_t newsize = 100;
                size_t origsize = strlen(search.c_str()) + 1;
                size_t convertedChars = 0;
                wchar_t wcstring[newsize];
                mbstowcs(wcstring, search.c_str(), newsize);
                wcscat(wcstring, L" (wchar_t *)");

                    while(fgetws( wides, sizeof wides, file ) != NULL) {
                        p = wcsstr(wides, L"error");
                        q = wcsstr (wides, L"Fatal");
                        if (p != NULL ||
                            q != NULL) {
                            fputws(wides, stdout);
                }
            }
        }

    }
    closedir(dirp);
  }
printf("Press any key to continue.\n");
getch();
return (0);
}

I'm wondering if what I've tried to do is even legitimate.. it complies without error but just prints the first file name found and crashes. Apart from "WRONG" am I even remotely on the right track? As always any tips are appreciated :)

OK.. solved it by adding this:

DIR           *dirp;
  struct dirent *dir;
  dirp = opendir(".");
  if (dirp)
  [U]wchar_t d_name[260] = {0};[/U]

underlined what I was missing.. I had it defaulted as a string not wide string..

commented: Quite a proficient newbie, happy journey! +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.