How to convert C++ code to C.

Write a program in C that asks a user for the name of a file. The program should display the first 10 lines of the file on the screen (the "head" of the file). If the file has fewer than 10 lines, the entire file should be displayed, with a message indicating the entire file has been displayed.

    \//Run the program using visual studio 2010 vc++
/**
 *This program ask the user to enter the name of the text file
 *and displays the ten lines of the file and if the file having less than 10 line than
 *display the output that the total file has been displayed
 */
    //Header files
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
    //start of main funtion
int main()
{
        //variable declaration
    string filename;
    char ch;
        //char line[80];
        //create file stream object to the class fstream
    fstream fin;
        //ask for file name
    cout<<"Enter a filename"<
    cin>>filename;
        //open that file in read mode
    fin.open(filename,ios::in);
        //check the file is opened successfully
    if(!fin)
        {
        cout<<"Error while opening file "<
        }
    else
        {
        int counter=0;
            //checking for end of the file

        while((ch=fin.get())!=EOF && counter<=10 )
            {
                //getline funtion to take one line at a time
            if(ch=='\n' )
                {
                counter++;
                }
                //display to the screen
            cout<

            }
            //display the message if number of lines are less than ten
        if(counter<10)
            {
            cout<<<"------------------------------"<
            cout<<"ENTIRE FILE HAS BEEN DISPLAYED"<
            }

        }
        //to pause the output
    system("pause");
    return 0;
}
    //end of main function

Please help. Thanks.

Recommended Answers

All 10 Replies

This is supposed to be written in C? The following lines of code are C++ and need to be replaced with C code.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
string filename;
fstream fin;
cout<<"Enter a filename"<
cin>>filename;
fin.open(filename,ios::in);
cout<<"Error while opening file "<
fin.get()
cout<
cout<<<"------------------------------"<
cout<<"ENTIRE FILE HAS BEEN DISPLAYED"<

I see that every time you use cout, you have < at the end of the line. That makes no sense. Why? Lines end with ;

File reading in C: http://en.wikipedia.org/wiki/C_file_input/output

I made few changes, but still I can not able to read 10 lines. My program reads all the line untill end. So how do I read only 10 line of input file. Here is my code:

#include <stdio.h>

int main()
{
    char *inname;
    FILE *infile;
    char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
    char fileName[14];

    scanf("%s",fileName);
    infile=fopen(fileName,"r");

    if (!infile)
        {
        printf("Couldn't open file %s for reading.\n", inname);
        return 0;
        }

    while (fgets(line_buffer, sizeof(line_buffer), infile))
        {
                printf("%s\n", line_buffer);
        }
    return 0;
}

So how do I read only 10 line of input file.

Create a variable that counts to 10:

for (int i = 0; i < 10 && fgets(line_buffer, sizeof(line_buffer), infile); i++)
{
    printf("%s\n", line_buffer);
}

Thanks deceptikon. But it still reads the hole file. Here is my updates code:

#include <stdio.h>

int main()
{
    char *inname;
    FILE *infile;
    char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
    char fileName[14];

    scanf("%s",fileName);
    infile=fopen(fileName,"r");

    if (!infile)
        {
        printf("Couldn't open file %s for reading.\n", inname);
        return 0;
        }

    for (int i = 0; i < 10 && fgets(line_buffer, sizeof(line_buffer), infile); i++)
        {
        printf("%s\n", line_buffer);
        }
    return 0;
}

But it still reads the hole file.

If the code you posted is the code you're running, then the file has 10 lines or less. On the off chance that I'm stupid and failed to properly compile and run your code in my head, I tested it here and it worked perfectly.

Thanks. It works.

Why am I getting "Segmentation fault: 11". I have to compile this code by redirecting file from stdin like ./my < data.

If you're still using the above code, then inname hasn't been initialized. However, you won't have any problems with it unless you enter the case of a file not being able to be opened (ie. infile is NULL).

Thanks' deceptikon.

In hindsight, I should have mentioned that before. Oh well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.