0

So my goal is to create a console app using visual C++ lang which opens a file ( in my case it is a MIME file) and finds a certain string. In my case it sounds - Content-Disposition: attachment; filename="file.smth". And then the app shows the file.smth So here is what I have done. It has some problems that I am not able to find.When I run a console app It gets stuck at finding a filename.

#include "stdafx.h" 
#include <stdlib.h>
#include <string.h>




bool ShowFile(char * FileName, char * Name)
{
    FILE* file;
    if (fopen_s(&file, FileName, "rt") != 0) { return false; }
    while (!feof(file))
    {
        char AttName[100];
        int a = sscanf_s("Content-Disposition: attachment; filename=\"%[^\"]\"", AttName,_countof(AttName));
        Name = AttName;
    }
    fclose(file);
    return true;
}


int main(int argc, char* argv[])
{
    char FileName[100];
    if (argc == 2) strcpy_s(FileName, _countof(FileName), argv[1]);
    else {
        printf("Source file name: "); gets_s(FileName, _countof(FileName));
    }
    char Name[100];
    ShowFile(FileName, Name);
    printf("%s \n", Name);
    system("pause");
    return 0;
}    

Thank you for your attention!

matz
  • 626
  • 6
  • 18

1 Answers1

0

The function ShowFile can be improved.

Suggestion 1

If you would like the function to return the name of the file (file.smth) by the second argument, change it to std::string&.

//bool ShowFile(char * FileName, char * Name)
bool ShowFile(char * FileName, std::string& Name)

As the function stands right now, you are changing Name in the function to point to a local variable. But that has no impact on the calling function. The line

Name = AttName;

is completely useless.

Suggestion 2

You haven't added any code to read the data from the file. The line

int a = sscanf_s("Content-Disposition: attachment; filename=\"%[^\"]\"", AttName,_countof(AttName));

has the format specification as the first argument, not the string from which to read the data. It is missing the source string.

You need to add code to read lines of text and try to extract AttName from those lines.

Suggestion 3

Don't use while (!feof(file)). See Why is “while ( !feof (file) )” always wrong?.

You need something like:

char line[200];
while ( fgets(line, sizeof(line), file) )
{
    char AttName[100];
    int a = sscanf_s(line, "Content-Disposition: attachment; filename=\"%[^\"]\"", AttName,_countof(AttName));
    ...
}

Suggestion 4

Always check the returned value of scanf family of functions to make sure that the function was able to assign data to the variables. Don't assume it succeeded.

int a = sscanf_s(line, "Content-Disposition: attachment; filename=\"%[^\"]\"", AttName,_countof(AttName));
// Name = AttName;
if ( a == 1 )
{
   Name = AttName;
}

Suggestion 5

Add a flag to indicate that Name was successfully read.

Revised function

bool ShowFile(char * FileName, std::string& Name)
{
    bool status = false;
    FILE* file;
    if (fopen_s(&file, FileName, "rt") != 0) { return false; }
    char line[200];
    while (fgets(line, sizeof(line), file))
    {
        char AttName[100];
        int a = sscanf_s(line, "Content-Disposition: attachment; filename=\"%[^\"]\"", AttName,_countof(AttName));
        if ( a == 1 )
        {
           Name = AttName;

           // Got what we are looking for.
           // Set the status and break out of the loop.
           // There is no need to look for Name any more.
           status = true;
           break;
        }
    }
    fclose(file);

    return status;
}
Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270