Solved

Trying to read in from a file

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
need process to patch debian offline without internet 7 227
How to import linux vhd to sccvmm 1 152
Ubuntu 16.04 Emergency Boot 2 189
Set-Top-Box hardware and Open Source! 8 191
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 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