Solved

Trying to read in from a file

Posted on 2003-11-28
5
195 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now