Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Trying to read in from a file

Posted on 2003-11-28
5
Medium Priority
?
203 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
3 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 1000 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 1000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

877 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