| New Reply |
c programming question |
Share Thread | Thread Tools |
| Jun9-11, 04:17 PM | #1 |
|
|
c programming question
Hello! I am learning how to use file and argc and I am stuck on this assignment : write a program that sequentially displays on screen the contents of all of the files listed in the command line. Use argc to control a loop.
Here is my code, I'll try to enclose it in [ CODE ] [ /CODE ] tags if it doesn't work please explain how to do it: [#include <stdio.h> #include <stdlib.h> int main (int argc, char * argv[]) { int i; FILE *fp; for(i = 0; i < argc; i++) fp = fopen(argv[i],"r"); fprintf(fp, "Testing...\n"); return 0; } ] I can't check if it works because I don't have any file in the command line and I know too little about files, the book doesn't help much. Thanks |
| Jun9-11, 04:37 PM | #2 |
|
|
The code tags work fine for me:
Code:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char * argv[])
{
int i;
FILE *fp;
for(i = 0; i < argc; i++)
fp = fopen(argv[i],"r");
fprintf(fp, "Testing...\n");
return 0;
}
1) Which command(s) are inside the for(i=...) loop? Which are outside? 2) I think that argv[0] is the name of the program that is being executed, not the first parameter passed. I may be wrong there, but you may want to check that. |
| Jun9-11, 04:42 PM | #3 |
|
|
argv[0] is for the program name. Your program should use file names starting with argv[1]. The line
Code:
fprintf(fp, "Testing...\n"); You should have a call to fclose within the loop. Otherwise the files will remain open and your program will no longer have the FILE to close them, because it was overwritten with data for the next file. |
| Jun9-11, 05:05 PM | #4 |
|
Mentor
|
c programming question
I don't advise it, but you could make fprintf work like so:
Code:
fprintf(stdout, "Testing...\n"); This is functionally the same as Code:
printf("Testing...\n");
|
| Jun9-11, 05:46 PM | #5 |
|
|
Hey guys, thank u all!
So here is what I don't understand:will fopen open the file and show its content? then there is no use of printf... About putting fclose in the loop: the problem asks for the contents on the screen to stay displayed. Can anyone explain how does it work? |
| Jun9-11, 05:59 PM | #6 |
|
Mentor
|
|
| Jun9-11, 06:02 PM | #7 |
|
|
Once you print the content to the screen it will stay there. What you (schematically) do is
- open the file. That is what fopen() does. - copy the data from the file to the memory (i.e. reading the file content). You still have to figure how to do that. - copy the data from the memory to the screen (i.e. printing out the content). That is where the printf() comes in. - close the file. The content of the file has, ideally, not changed in any of those operations. You do not manipulate the data itself but create copies of it; modifying the file after having printed out the content would not alter the copy on the screen (and closing the file doesn't change its data, anyways). |
| Jun9-11, 06:52 PM | #8 |
|
|
If all you want to do is dump the file contents to the screen, then you could look at just looping until you get an end of file (EOF) from fgetc. Until you hit the end of the file, you just read a character with fgetc and print it out with fputc (or just putchar). There's probably no need to read the whole file into memory before printing it, anyways.
As Timo points out, make sure you know what's in the for loop and what's not. I would suggest you use curly braces to mark blocks explicitly. There are various reasons, but mostly it's just clearer and helps prevent some common errors. It's optional, but I prefer using curly braces even when there's only one line inside the loop/conditional/etc. Also don't forget to handle errors. For example, maybe the file doesn't exist. Or maybe you don't have the proper file permissions to read that file. |
| Jun9-11, 07:10 PM | #9 |
|
|
Thank u so much!!!!
Just one last question. I'd like to check if the program works but I don't know how to create a file in the command line. Any ideas? Thanks! |
| Jun9-11, 07:20 PM | #10 |
|
Recognitions:
|
Hi stellina!
![]() echo "I like stellina" >t.txt on the command line, and you'll have a file named t.txt. ;) |
| Jun9-11, 07:49 PM | #11 |
|
Mentor
|
If you create a file as suggested by I like Serena, make sure that you move the file to the same directory as your executable, or your program won't be able to find this file.
|
| Jun9-11, 07:55 PM | #12 |
|
|
You can cd to the program's directory first or just include the full path to the directory when running echo to avoid what Mark44 pointed out.
|
| Jun9-11, 08:24 PM | #13 |
|
|
I feel compelled to point out an obvious way to create a more elaborate text file. You can just use an editor. The one you used to make your source file will probably do the job.
For simple things, I often just use the vi editor, but that takes a little getting used to. I assume you're on some kind of Linux system? If you have a gnome desktop, try gedit. For KDE, you can use kate. Both are very capable editors. So let's say I want to create a text file with gedit, and I'm in the proper directory. I would just type this at the prompt (with whatever filename you want): gedit textfile.txt Replace 'gedit' with 'kate' to use that editor instead. |
| Jun10-11, 08:47 AM | #14 |
|
|
Hey guys, thanks a lot but I am still stuck because I am using xcode as a compiler and there is a different procedure to create a file. I can't figure it out! Is there anyone using xcode that can guide me step by step? Thanks!
|
| Jun10-11, 10:18 AM | #15 |
|
Mentor
|
The compiler has nothing to do with creating a file you use for input. You can use any of the methods given by I like Serena, jhae2,718, or Grep to create your input file.
|
| Jun10-11, 10:34 AM | #16 |
|
Recognitions:
|
I'm not really familiar with it, but I can tell you that with xcode you should be able to create a text file. Otherwise you can create a text file with TextEdit which should be included somewhere on your Apple. I can't really tell you where to put the file, or how to run your program in a way that it can find the file. Perhaps someone familiar with xcode and Apple can!
|
| New Reply |
| Thread Tools | |
Similar Threads for: c programming question
|
||||
| Thread | Forum | Replies | ||
| Recommended programming language (and texts) for middle-school beginner programming | Programming & Comp Sci | 9 | ||
| Programming question | Engineering, Comp Sci, & Technology Homework | 5 | ||
| Programming Question | Computing & Technology | 1 | ||
| Programming Question | Computing & Technology | 9 | ||
| C programming question | Computing & Technology | 8 | ||