Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trying to read in from a file

Posted on 2003-11-28
5
Medium Priority
?
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

704 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