473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Open, Read, Print an input file

I'm a beginner for C programming. For a part of my project, I have to take an input file from the user and be able to print it onto the screen. This is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. char filename[10];
  2.  
  3. FILE *ifp;
  4. printf("Enter the name of the file to analyze :");
  5. scanf("%s", filename);
  6. printf("\n");
  7.  
  8. ifp = fopen(filename, "r");
  9. if (ifp == NULL)
  10. {
  11.    fprintf(stderr, "Could not open file: '%s' ", filename);
  12.    exit (-1);
  13. }
  14.  
  15. int c = fgetc(ifp);
  16.  
  17. while (c != EOF)
  18. {
  19.    printf("%c", ifp);
  20. }
  21.  
  22. fclose(ifp);
  23.  
  24.  
However... I don't think the while loop is working properly especially when I get an argument error. How can I fix this?

Thanks!
Nov 5 '06 #1
1 8477
I'm a beginner for C programming. For a part of my project, I have to take an input file from the user and be able to print it onto the screen. This is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. char filename[10];
  2.  
  3. FILE *ifp;
  4. printf("Enter the name of the file to analyze :");
  5. scanf("%s", filename);
  6. printf("\n");
  7.  
  8. ifp = fopen(filename, "r");
  9. if (ifp == NULL)
  10. {
  11.    fprintf(stderr, "Could not open file: '%s' ", filename);
  12.    exit (-1);
  13. }
  14.  
  15. int c = fgetc(ifp);
  16.  
  17. while (c != EOF)
  18. {
  19.  
  20.  
  21. //printf("%c", c);
  22. //c=fgetc(ifp);
  23.  /*  printf("%c", ifp);*/
  24.  
  25. }
  26.  
  27. fclose(ifp);
  28.  
  29.  
However... I don't think the while loop is working properly especially when I get an argument error. How can I fix this?

Thanks!

check the above commented lines!! guess it works
Nov 5 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: haynesc | last post by:
Hi, I'm having a problem where when trying to open a file in write mode, I get an IOError stating no such file or directory. I'm calling an external program which takes an input file and...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
14
by: Rochester | last post by:
Hi, I just found out that the general open file mechanism doesn't work for named pipes (fifo). Say I wrote something like this and it simply hangs python: #!/usr/bin/python import os
1
by: piratelord | last post by:
I've an existing program I need to tweak that can open a non-text file at low level, find certain strings and replace with another, and write back to a new file. The string replacement bit isn't a...
4
by: varsha desai | last post by:
Hello there, I want to change some data(which is in one line only) of text file using VB 6.0. Which is the best method for it? Another question is I want to delete last two, three lines...
0
by: Ofelia | last post by:
Hi, I'm new to this forum and to Perl language but I would like to ask for your help. I'm working in Linux and the files I need to process are in the format “file.gz”. I created a script which...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
6
by: sebastian.noack | last post by:
Hi, is there a way to or at least a reason why I can not use tarfile to create a gzip or bunzip2 compressed archive in the memory? You might might wanna answer "use StringIO" but this isn't...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.