Keyword

Result: 265 questions

What are some of the key new features in HTML5?

Answer:

Key new features of HTML5 include:

  • Improved support for embedding graphics, audio, and video content via the new , , and  tags.

  • Extensions to the JavaScript API such as geolocation and drag-and-drop as well for storage and caching.

  • Introduction of “web workers”.

  • Several new semantic tags were also added to complement the structural logic of modern web applications. These include the 

    , , , , , , and  tags.

     

  • New form controls, such as , , , , , and .

View

Discuss the differences between an HTML specification and a browser’s implementation thereof.

Answer:

HTML specifications such as HTML5 define a set of rules that a document must adhere to in order to be “valid” according to that specification. In addition, a specification provides instructions on how a browser must interpret and render such a document.

A browser is said to “support” a specification if it handles valid documents according to the rules of the specification. As of yet, no browser supports all aspects of the HTML5 specification (although all of the major browser support most of it), and as a result, it is necessary for the developer to confirm whether the aspect they are making use of will be supported by all of the browsers on which they hope to display their content. This is why cross-browser support continues to be a headache for developers, despite the improved specificiations.

In addition, while HTML5 defines some rules to follow for an invalid HTML5 document (i.e., one that contains syntactical errors), invalid documents may contain anything, and it is impossible for the specification to handle all possibilities comprehensively. Thus, many decisions about how to handle malformed documents are left up to the browser.

View

Explain what is GO?

Answer:

GO is an open source programming language which makes it easy to build simple, reliable and efficient software. Programs are constructed from packages, whose properties allow efficient management of dependencies.

View

What do you think are limitation of Django Object relation mapping(ORM) ?

Answer:

If the data is complex and consists of multiple joins using the SQL  will be clearer.

If Performance is a concern for your, ORM aren’t your choice. Genrally. Object-relation-mapping are considered good option to construct an optimized query, SQL has an upper hand when compared to ORM.

View

Which of the following is NOT a correct constructer for ArrayAdapter?

Quiz

What are the __construct() and __destruct() methods in a PHP class?

Answer:

All objects in PHP have Constructor and Destructor methods built-in. The Constructor method is called immediately after a new instance of the class is being created, and it’s used to initialize class properties. The Destructor method takes no parameters.

Understanding these two in PHP means that the candidate knows the very basics of OOP in PHP.

View

Suppose that you have to implement a class named Dragonball. This class must have an attribute named ballCount (which starts from 0) and a method iFoundaBall. When iFoundaBall is called, ballCount is increased by one. If the value of ballCount is equal to seven, then the message You can ask your wish is printed, and ballCount is reset to 0. How would you implement this class?

Answer:

<?php
class dragonBall{
  private $ballCount;

  public function __construct(){
    $this->ballCount=0;
  }

  public function iFoundaBall(){
    $this->ballCount++;
    if($this->ballCount===7){
      echo "You can ask for your wish.";
      $this->ballCount=0;
    }
  }
}
?>

This question will evaluate a candidate’s knowledge about OOP.

View

What PSR Standards do you follow? Why would you follow a PSR standard?

Answer:

You should folow a PSR because coding standards often vary between developers and companies. This can cause issues when reviewing or fixing another developer’s code and finding a code structure that is different from yours. A PSR standard can help streamline the expectations of how the code should look, thus cutting down confusion and in some cases syntax errors.

View

What is a local block?

Answer:

A local block is any portion of a C program that is enclosed by the left brace ({) and the right brace (}). A C function contains left and right braces, and therefore anything between the two braces is contained in a local block. An if statement or a switch statement can also contain braces, so the portion of code between these two braces would be considered a local block.

Additionally, you might want to create your own local block without the aid of a C function or keyword construct. This is perfectly legal. Variables can be declared within local blocks, but they must be declared only at the beginning of a local block. Variables declared in this manner are visible only within the local block. Duplicate variable names declared within a local block take precedence over variables with the same name declared outside the local block. Here is an example of a program that uses local blocks:

 


#include <stdio.h>
void main(void);
void main()
{
     /* Begin local block for function main() */
     int test_var = 10;
     printf("Test variable before the if statement: %d\n", test_var);
     if (test_var > 5)
     {
          /* Begin local block for "if" statement */
          int test_var = 5;
          printf("Test variable within the if statement: %d\n",
                 test_var);
          {
               /* Begin independent local block (not tied to
                  any function or keyword) */
               int test_var = 0;
               printf(
               "Test variable within the independent local block:%d\n",
               test_var);
          }
          /* End independent local block */
     }
     /* End local block for "if" statement */
     printf("Test variable after the if statement: %d\n", test_var);
}
/* End local block for function main() */

This example program produces the following output:

Test variable before the if statement: 10

Test variable within the if statement: 5

Test variable within the independent local block: 0

Test variable after the if statement: 10

Notice that as each test_var was defined, it took precedence over the previously defined test_var. Also notice that when the if statement local block had ended, the program had reentered the scope of the original test_var, and its value was 10.

View

What is the difference between goto and long jmp( ) and setjmp()?

Answer:

goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution. Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program.

goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement gotos between functions. Here is an example of a goto statement:

void bad_programmers_function(void)
{
     int x;
     printf("Excuse me while I count to 5000...\n");
     x = 1;
     while (1)
     {
          printf("%d\n", x);
          if (x == 5000)
               goto all_done;
          else
               x = x + 1;
     }
all_done:
     printf("Whew! That wasn't so bad, was it?\n");
}

This example could have been written much better, avoiding the use of a goto statement. Here is an example of an improved implementation:

void better_function(void)
{
     int x;
     printf("Excuse me while I count to 5000...\n");
     for (x=1; x<=5000; x++)
          printf("%d\n", x);
     printf("Whew! That wasn't so bad, was it?\n");
}

As previously mentioned, the longjmp() and setjmp() functions implement a nonlocal goto. When your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program's state as it was when you called setjmp(). Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function.

However, there is a major drawback to using these functions: your program, when restored to its previously saved state, will lose its references to any dynamically allocated memory between the longjmp() and the setjmp(). This means you will waste memory for every malloc() or calloc() you have implemented between your longjmp() and setjmp(), and your program will be horribly inefficient. It is highly recommended that you avoid using functions such as longjmp() and setjmp() because they, like the goto statement, are quite often an indication of poor programming practice.

Here is an example of the longjmp() and setjmp() functions:

#include <stdio.h>
#include <setjmp.h>
jmp_buf saved_state;
void main(void);
void call_longjmp(void);
void main(void)
{
     int ret_code;
     printf("The current state of the program is being saved...\n");
     ret_code = setjmp(saved_state);
     if (ret_code == 1)
     {
          printf("The longjmp function has been called.\n");
          printf("The program's previous state has been restored.\n");
          exit(0);
     }
     printf("I am about to call longjmp and\n");
     printf("return to the previous program state...\n");
     call_longjmp();
}
void call_longjmp(void)
{
     longjmp(saved_state, 1);
}
View

Can an array be an lvalue?

Answer:

Is an array an expression to which we can assign a value? The answer to this question is no, because an array is composed of several separate array elements that cannot be treated as a whole for assignment purposes. The following statement is therefore illegal:

int x[5], y[5];

x = y;

You could, however, use a for loop to iterate through each element of the array and assign values individually, such as in this example:

int i;
int x[5];
int y[5];
...
for (i=0; i<5; i++)
     x[i] = y[i]
...

Additionally, you might want to copy the whole array all at once. You can do so using a library function such as the memcpy() function, which is shown here:

memcpy(x, y, sizeof(y));

It should be noted here that unlike arrays, structures can be treated as lvalues. Thus, you can assign one structure variable to another structure variable of the same type, such as this:

typedef struct t_name
{
     char last_name[25];
     char first_name[15];
     char middle_init[2];
} NAME;
...
NAME my_name, your_name;
...
your_name = my_name;
...

In the preceding example, the entire contents of the my_name structure were copied into the your_namestructure. This is essentially the same as the following line:

memcpy(your_name, my_name, sizeof(your_name));

View

What will the preprocessor do for a program?

Answer:

The C preprocessor is used to modify your program according to the preprocessor directives in your source code. A preprocessor directive is a statement (such as #define) that gives the preprocessor specific instructions on how to modify your source code. The preprocessor is invoked as the first part of your compiler program's compilation step. It is usually hidden from the programmer because it is run automatically by the compiler.

The preprocessor reads in all of your include files and the source code you are compiling and creates a preprocessed version of your source code. This preprocessed version has all of its macros and constant symbols replaced by their corresponding code and value assignments. If your source code contains any conditional preprocessor directives (such as #if), the preprocessor evaluates the condition and modifies your source code accordingly.

Here is an example of a program that uses the preprocessor extensively:

#include <stdio.h>
#define TRUE         1
#define FALSE        (!TRUE)
#define GREATER(a,b) ((a) > (b) ? (TRUE) : (FALSE))
#define PIG_LATIN    FALSE
void main(void);
void main(void)
{
     int x, y;
#if PIG_LATIN
     printf("Easeplay enternay ethay aluevay orfay xnay: ");
     scanf("%d", &x);
     printf("Easeplay enternay ethay aluevay orfay ynay: ");
     scanf("%d", &y);
#else
     printf("Please enter the value for x: ");
     scanf("%d", &x);
     printf("Please enter the value for y: ");
     scanf("%d", &y);
#endif
     if (GREATER(x,y) == TRUE)
     {
#if PIG_LATIN
          printf("xnay islay eatergray anthay ynay!\n");
#else
          printf("x is greater than y!\n");
#endif
     }
     else
     {
     #if PIG_LATIN
          printf("xnay islay otnay eatergray anthay ynay!\n");
#else
          printf("x is not greater than y!\n");
#endif
     }
}

This program uses preprocessor directives to define symbolic constants (such as TRUEFALSE, and PIG_LATIN), a macro (such as GREATER(a,b)), and conditional compilation (by using the #if statement). When the preprocessor is invoked on this source code, it reads in the stdio.h file and interprets its preprocessor directives, then it replaces all symbolic constants and macros in your program with the corresponding values and code. Next, it evaluates whether PIG_LATIN is set to TRUE and includes either the pig latin text or the plain English text.

If PIG_LATIN is set to FALSE, as in the preceding example, a preprocessed version of the source code would look like this:

/* Here is where all the include files
   would be expanded. */
void main(void)
{
     int x, y;
     printf("Please enter the value for x: ");
     scanf("%d", &x);
     printf("Please enter the value for y: ");
     scanf("%d", &y);
     if (((x) > (y) ? (1) : (!1)) == 1)
     {
          printf("x is greater than y!\n");
     }
     else
     {
          printf("x is not greater than y!\n");
     }
}

This preprocessed version of the source code can then be passed on to the compiler. If you want to see a preprocessed version of a program, most compilers have a command-line option or a standalone preprocessor program to invoke only the preprocessor and save the preprocessed version of your source code to a file. This capability can sometimes be handy in debugging strange errors with macros and other preprocessor directives, because it shows your source code after it has been run through the preprocessor.

View

Is it better to use a macro or a function?

Answer:

The answer depends on the situation you are writing code for. Macros have the distinct advantage of being more efficient (and faster) than functions, because their corresponding code is inserted directly into your source code at the point where the macro is called. There is no overhead involved in using a macro like there is in placing a call to a function. However, macros are generally small and cannot handle large, complex coding constructs. A function is more suited for this type of situation.

Additionally, macros are expanded inline, which means that the code is replicated for each occurrence of a macro. Your code therefore could be somewhat larger when you use macros than if you were to use functions.

Thus, the choice between using a macro and using a function is one of deciding between the tradeoff of faster program speed versus smaller program size. Generally, you should use macros to replace small, repeatable code sections, and you should use functions for larger coding tasks that might require several lines of code.

View

What is the concatenation operator?

Answer:

The concatenation operator (##) is used to concatenate (combine) two separate strings into one single string. The concatenation operator is often used in C macros, as the following program demonstrates:

#include <stdio.h>
#define SORT(x) sort_function ## x
void main(void);
void main(void)
{
     char* array;
     int   elements, element_size;
     ...
     SORT(3)(array, elements, element_size);
     ...
}

In the preceding example, the SORT macro uses the concatenation operator to combine the strings sort_function and whatever is passed in the parameter x. This means that the line

SORT(3)(array, elements, element_size);

is run through the preprocessor and is translated into the following line:

sort_function3(array, elements, element_size);

As you can see, the concatenation operator can come in handy when you do not know what function to call until runtime. Using the concatenation operator, you can dynamically construct the name of the function you want to call, as was done with the SORT macro.

View

Why can't constant values be used to define an array's initial size?

Answer:

There are times when constant values can be used and there are times when they can't. A C program can use what C considers to be constant expressions, but not everything C++ would accept.

When defining the size of an array, you need to use a constant expression. A constant expression will always have the same value, no matter what happens at runtime, and it's easy for the compiler to figure out what that value is. It might be a simple numeric literal:

char a[ 512 ];

Or it might be a "manifest constant" defined by the preprocessor:

#define MAX     512
/* ... */
char    a[ MAX ];

Or it might be a sizeof:

char a[ sizeof( struct cacheObject ) ];

Or it might be an expression built up of constant expressions:

char buf[ sizeof( struct cacheObject ) * MAX ];

Enumerations are allowed too.

An initialized const int variable is not a constant expression in C:

int max = 512; /* not a constant expression in C */

char buffer[ max ]; /* not valid C */

Using const ints as array sizes is perfectly legal in C++; it's even recommended. That puts a burden on C++ compilers (to keep track of the values of const int variables) that C compilers don't need to worry about. On the other hand, it frees C++ programs from using the C preprocessor quite so much.

View

How do you list files in a directory?

Answer:

Unfortunately, there is no built-in function provided in the C language such as dir_list() that would easily provide you with a list of all files in a particular directory. By utilizing some of C's built-in directory functions, however, you can write your own dir_list() function.

First of all, the include file dos.h defines a structure named find_t, which represents the structure of the DOS file entry block. This structure holds the name, time, date, size, and attributes of a file. Second, your C compiler library contains the functions _dos_findfirst() and _dos_findnext(), which can be used to find the first or next file in a directory.

The _dos_findfirst() function requires three arguments. The first argument is the file mask for the directory list. A mask of *.* would be used to list all files in the directory. The second argument is an attribute mask, defining which file attributes to search for. For instance, you might want to list only files with the Hidden or Directory attributes. The last argument of the _dos_findfirst() function is a pointer to the variable that is to hold the directory information (the find_t structure variable).

The second function you will use is the _dos_findnext() function. Its only argument is a pointer to thefind_t structure variable that you used in the _dos_findfirst() function. Using these two functions and the find_t structure, you can iterate through the directory on a disk and list each file in the directory. Here is the code to perform this task:

#include <stdio.h>
#include <direct.h>
#include <dos.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
typedef struct find_t FILE_BLOCK;
void main(void);
void main(void)
{
     FILE_BLOCK f_block;      /* Define the find_t structure variable */
     int ret_code;     /* Define a variable to store the return codes */
     printf("\nDirectory listing of all files in this directory:\n\n");
     /* Use the "*.*" file mask and the 0xFF attribute mask to list
        all files in the directory, including system files, hidden
        files, and subdirectory names. */
     ret_code = _dos_findfirst("*.*", 0xFF, &f_block);
     /* The _dos_findfirst() function returns a 0 when it is successful
        and has found a valid filename in the directory. */
     while (ret_code == 0)
     {
          /* Print the file's name */
          printf("%-12s\n", f_block.name);
          /* Use the _dos_findnext() function to look
             for the next file in the directory. */
          ret_code = _dos_findnext(&f_block);
     }
     printf("\nEnd of directory listing.\n");
}
View

How do you list a file's date and time?

Answer:

A file's date and time are stored in the find_t structure returned from the _dos_findfirst() and_dos_findnext() functions.

The date and time stamp of the file is stored in the find_t.wr_date and find_t.wr_time structure members. The file date is stored in a two-byte unsigned integer as shown here:

Element   Offset   Range
Seconds - 5 bits - 0-9 (multiply by 2 to get the seconds value)
Minutes - 6 bits - 0-59
Hours - 5 bits - 0-23

Similarly, the file time is stored in a two-byte unsigned integer, as shown here:

Element   Offset   Range
Day - 5 bits - 1-31
Month - 4 bits - 1-12
Year - 7 bits - 0-127 (add the value "1980" to get the year value)

Because DOS stores a file's seconds in two-second intervals, only the values 0 to 29 are needed. You simply multiply the value by 2 to get the file's true seconds value. Also, because DOS came into existence in 1980, no files can have a time stamp prior to that year. Therefore, you must add the value "1980" to get the file's true year value.

The following example program shows how you can get a directory listing along with each file's date and time stamp:

#include <stdio.h>
#include <direct.h>
#include <dos.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
typedef struct find_t FILE_BLOCK;
void main(void);
void main(void)
{
     FILE_BLOCK f_block;   /* Define the find_t structure variable */
     int ret_code;         /* Define a variable to store return codes */
     int hour;             /* We're going to use a 12-hour clock! */
     char* am_pm;          /* Used to print "am" or "pm" */
     printf("\nDirectory listing of all files in this directory:\n\n");
     /* Use the "*.*" file mask and the 0xFF attribute mask to list
        all files in the directory, including system files, hidden
        files, and subdirectory names. */
     ret_code = _dos_findfirst("*.*", 0xFF, &f_block);
     /* The _dos_findfirst() function returns a 0 when it is successful
        and has found a valid filename in the directory. */
     while (ret_code == 0)
     {
          /* Convert from a 24-hour format to a 12-hour format. */
          hour = (f_block.wr_time >> 11);
          if (hour > 12)
          {
               hour  = hour - 12;
               am_pm = "pm";
          }
          else
               am_pm = "am";
          /* Print the file's name, date stamp, and time stamp. */
          printf("%-12s  %02d/%02d/%4d  %02d:%02d:%02d %s\n",
                    f_block.name,                      /* name  */
                    (f_block.wr_date >> 5) & 0x0F,     /* month */
                    (f_block.wr_date) & 0x1F,          /* day   */
                    (f_block.wr_date >> 9) + 1980,     /* year  */
                    hour,                              /* hour  */
                    (f_block.wr_time >> 5) & 0x3F,     /* minute  */
                    (f_block.wr_time & 0x1F) * 2,      /* seconds */
                    am_pm);
          /* Use the _dos_findnext() function to look
             for the next file in the directory. */
          ret_code = _dos_findnext(&f_block);
     }
     printf("\nEnd of directory listing.\n");
}

Notice that a lot of bit-shifting and bit-manipulating had to be done to get the elements of the time variable and the elements of the date variable. If you happen to suffer from bitshiftophobia (fear of shifting bits), you can optionally code the preceding example by forming a union between the find_t structure and your own user-defined structure, such as this:

/* This is the find_t structure as defined by ANSI C. */
struct find_t
{
     char reserved[21];
     char attrib;
     unsigned wr_time;
     unsigned wr_date;
     long size;
     char name[13];
}
/* This is a custom find_t structure where we
   separate out the bits used for date and time. */
struct my_find_t
{
     char reserved[21];
     char attrib;
     unsigned seconds:5;
     unsigned minutes:6;
     unsigned hours:5;
     unsigned day:5;
     unsigned month:4;
     unsigned year:7;
     long size;
     char name[13];
}
/* Now, create a union between these two structures
   so that we can more easily access the elements of
   wr_date and wr_time. */
union file_info
{
     struct find_t ft;
     struct my_find_t mft;
}

Using the preceding technique, instead of using bit-shifting and bit-manipulating, you can now extract date and time elements like this:

...
file_info my_file;
...
printf("%-12s  %02d/%02d/%4d  %02d:%02d:%02d %s\n",
          my_file.mft.name,             /* name    */
          my_file.mft.month,            /* month   */
          my_file.mft.day,              /* day     */
          (my_file.mft.year + 1980),    /* year    */
          my_file.mft.hours,            /* hour    */
          my_file.mft.minutes,          /* minute  */
          (my_file.mft.seconds * 2),    /* seconds */
          am_pm);
View

How do you sort filenames in a directory?

Answer:

When you are sorting the filenames in a directory, the one-at-a-time approach does not work. You need some way to store the filenames and then sort them when all filenames have been obtained. This task can be accomplished by creating an array of pointers to find_t structures for each filename that is found. As each filename is found in the directory, memory is allocated to hold the find_t entry for that file. When all filenames have been found, the qsort() function is used to sort the array of find_t structures by filename.

The qsort() function can be found in your compiler's library. This function takes four parameters: a pointer to the array you are sorting, the number of elements to sort, the size of each element, and a pointer to a function that compares two elements of the array you are sorting. The comparison function is a user-defined function that you supply. It returns a value less than zero if the first element is less than the second element, greater than zero if the first element is greater than the second element, or zero if the two elements are equal. Consider the following example:

#include <stdio.h>
#include <direct.h>
#include <dos.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
typedef struct find_t FILE_BLOCK;
int  sort_files(FILE_BLOCK**, FILE_BLOCK**);
void main(void);
void main(void)
{
     FILE_BLOCK f_block;       /* Define the find_t structure variable */
     int ret_code;             /* Define a variable to store the return
                                  codes */
     FILE_BLOCK** file_list;   /* Used to sort the files */
     int file_count;           /* Used to count the files */
     int x;                    /* Counter variable */
     file_count = -1;
     /* Allocate room to hold up to 512 directory entries. */
     file_list = (FILE_BLOCK**) malloc(sizeof(FILE_BLOCK*) * 512);
     printf("\nDirectory listing of all files in this directory:\n\n");
     /* Use the "*.*" file mask and the 0xFF attribute mask to list
        all files in the directory, including system files, hidden
        files, and subdirectory names. */
     ret_code = _dos_findfirst("*.*", 0xFF, &f_block);
     /* The _dos_findfirst() function returns a 0 when it is successful
        and has found a valid filename in the directory. */
     while (ret_code == 0 && file_count < 512)
     {
          /* Add this filename to the file list */
          file_list[++file_count] =
              (FILE_BLOCK*) malloc(sizeof(FILE_BLOCK));
          *file_list[file_count] = f_block;
          /* Use the _dos_findnext() function to look
             for the next file in the directory. */
          ret_code = _dos_findnext(&f_block);
     }
     /* Sort the files */
     qsort(file_list, file_count, sizeof(FILE_BLOCK*), sort_files);
     /* Now, iterate through the sorted array of filenames and
        print each entry. */
     for (x=0; x<file_count; x++)
     {
          printf("%-12s\n", file_list[x]->name);
     }
     printf("\nEnd of directory listing.\n");
}
int sort_files(FILE_BLOCK** a, FILE_BLOCK** b)
{
     return (strcmp((*a)->name, (*b)->name));
}

This example uses the user-defined function named sort_files() to compare two filenames and return the appropriate value based on the return value from the standard C library function strcmp(). Using this same technique, you can easily modify the program to sort by date, time, extension, or size by changing the element on which the sort_files() function operates.

View

How do you determine a file's attributes?

Answer:

The file attributes are stored in the find_t.attrib structure member. This structure member is a single character, and each file attribute is represented by a single bit. Here is a list of the valid DOS file attributes:

Value   Description   Constant
0x00 - Normal - (none)
0x01 - Read Only - FA_RDONLY
0x02 - Hidden File - FA_HIDDEN
0x04 - System File - FA_SYSTEM
0x08 - Volume Label - FA_LABEL
0x10 - Subdirectory - FA_DIREC
0x20 - Archive File - FA_ARCHIVE

To determine the file's attributes, you check which bits are turned on and map them corresponding to the preceding table. For example, a read-only hidden system file will have the first, second, and third bits turned on. A "normal" file will have none of the bits turned on. To determine whether a particular bit is turned on, you do a bit-wise AND with the bit's constant representation.

The following program uses this technique to print a file's attributes:

#include <stdio.h>
#include <direct.h>
#include <dos.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
typedef struct find_t FILE_BLOCK;
void main(void);
void main(void)
{
     FILE_BLOCK f_block;  /* Define the find_t structure variable */
     int ret_code;     /* Define a variable to store the return codes */
     printf("\nDirectory listing of all files in this directory:\n\n");
     /* Use the "*.*" file mask and the 0xFF attribute mask to list
        all files in the directory, including system files, hidden
        files, and subdirectory names. */
     ret_code = _dos_findfirst("*.*", 0xFF, &f_block);
     /* The _dos_findfirst() function returns a 0 when
        it is successful and has found a valid filename
        in the directory. */
     while (ret_code == 0)
     {
          /* Print the file's name */
          printf("%-12s  ", f_block.name);
          /* Print the read-only attribute */
          printf("%s ", (f_block.attrib & FA_RDONLY) ? "R" : ".");
          /* Print the hidden attribute */
          printf("%s ", (f_block.attrib & FA_HIDDEN) ? "H" : ".");
          /* Print the system attribute */
          printf("%s ", (f_block.attrib & FA_SYSTEM) ? "S" : ".");
          /* Print the directory attribute */
          printf("%s ", (f_block.attrib & FA_DIREC)  ? "D" : ".");
          /* Print the archive attribute */
          printf("%s\n", (f_block.attrib & FA_ARCH)  ? "A" : ".");
          /* Use the _dos_findnext() function to look
             for the next file in the directory. */
          ret_code = _dos_findnext(&f_block);
     }
     printf("\nEnd of directory listing.\n");
}
View

How can I read and write comma-delimited text?

Answer:

Many of today's popular programs use comma-delimited text as a means of transferring data from one program to another, such as the exported data from a spreadsheet program that is to be imported by a database program. Comma-delimited means that all data (with the exception of numeric data) is surrounded by double quotation marks ("") followed by a comma. Numeric data appears as-is, with no surrounding double quotation marks. At the end of each line of text, the comma is omitted and a newline is used.

To read and write the text to a file, you would use the fprintf() and fscanf() standard C library functions. The following example shows how a program can write out comma-delimited text and then read it back in.

#include <stdio.h>
#include <string.h>
typedef struct name_str
{
     char       first_name[15];
     char       nick_name[30];
     unsigned years_known;
} NICKNAME;
NICKNAME nick_names[5];
void main(void);
void set_name(unsigned, char*, char*, unsigned);
void main(void)
{
     FILE*     name_file;
     int       x;
     NICKNAME tmp_name;
     printf("\nWriting data to NICKNAME.DAT, one moment please...\n");
     /* Initialize the data with some values... */
     set_name(0,    "Sheryl",      "Basset",      26);
     set_name(1,    "Joel",        "Elkinator",    1);
     set_name(2,    "Cliff",       "Shayface",    12);
     set_name(3,    "Lloyd",       "Lloydage",    28);
     set_name(4,    "Scott",       "Pie",          9);
     /* Open the NICKNAME.DAT file for output in text mode. */
     name_file = fopen("NICKNAME.DAT", "wt");
     /* Iterate through all the data and use the fprintf() function
        to write the data to a file. */
     for (x=0; x<5; x++)
     {
          fprintf(name_file, "\"%s\", \"%s\", %u\n",
                      nick_names[x].first_name,
                      nick_names[x].nick_name,
                      nick_names[x].years_known);
     }
     /* Close the file and reopen it for input. */
     fclose(name_file);
     printf("\nClosed NICKNAME.DAT, reopening for input...\n");
     name_file = fopen("NICKNAME.DAT", "rt");
     printf("\nContents of the file NICKNAME.DAT:\n\n");
     /* Read each line in the file using the scanf() function
        and print the file's contents. */
     while (1)
     {
          fscanf(name_file, "%s %s %u",
                     tmp_name.first_name,
                     tmp_name.nick_name,
                     &tmp_name.years_known);
          if (feof(name_file))
               break;
          printf("%-15s %-30s %u\n",
                     tmp_name.first_name,
                     tmp_name.nick_name,
                     tmp_name.years_known);
     }
     fclose(name_file);
}
void set_name(unsigned name_num, char* f_name, char* n_name, unsigned years)
{
     strcpy(nick_names[name_num].first_name, f_name);
     strcpy(nick_names[name_num].nick_name,  n_name);
     nick_names[name_num].years_known = years;
}
View

© 2017 QuizBucket.org