Solved

Trying to read in from a file

Posted on 2003-11-28
5
199 Views
Last Modified: 2012-05-04
I need a simple way (command line program, if there is a way) to ask for a certain X number of bytes from a file and have it print out the first X characters to standard output.  It's for testing.

Scorpantula
0
Comment
Question by:scorpantula
  • 2
5 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 250 total points
ID: 9840079
#include <stdio.h>

int main(int argc, char * argv[])
{
    FILE * in;
    int limit;
    int i;

    if (argc != 3)
    {
        fprintf(stderr, "Usage: %s <filename> <byte count>\n", argv[0]);
        exit(-1);
    }

    in = fopen(argv[1], "r");
    if (in == NULL)
    {
        fprintf(stderr, "Cannot open file %s\n", argv[1]);
        exit(-2);
    }

    limit = atoi(argv[2]);

    for (i=0; i < limit; i++)
    {
        putchar(fgetc(in));
    }

    putchar('\n');
    fclose(in);
}

Save this to a file (e.g. test.c), then run "make test", which will create the executable test. Then run the command with two arguments on the command line: The first one is the file name, and the second one the number of characters.

e.g.:

./test test.c 45

0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9840089
Forgot to say this: The program woks for text files for binary files you need to change the output function, but this depends on what you want to see in the output. (e.g. hex dump, octal values, ...)
0
 
LVL 5

Assisted Solution

by:mtmike
mtmike earned 250 total points
ID: 9840616
Use head (man head), eg

head -c X FILE

to print out the first X bytes of FILE.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question