Learn How to Use File and Argc in C Programming: Step-by-Step Guide

In summary: If you're using a GUI interface, then you most likely have gedit, kedit, or something like that available. Just create your text file and save it to the same directory as your program.If you are developing on a remote server and using ssh to log in, then you can forward the X11 interface over the ssh connection and run a GUI editor to create your text file. For example, ssh -Y -l username hostname will get you logged into the server. Then you can use gedit to create your text file, and it will appear on your computer.For that matter, you can also run the entire program on your computer and just mount the remote file system where your program (and files) reside
  • #1
stellina
8
0
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,"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
 
Physics news on Phys.org
  • #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;
}
Without knowing much of the old C commands or what your program is supposed to do (the "Testing..." reads as if you don't even expect this program to do what it is supposed to): two comments:
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.
 
  • #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");

is incorrect - fprintf is for writing to files

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.
 
  • #4
I don't advise it, but you could make fprintf work like so:
Code:
fprintf(stdout, "Testing...\n");

stdout is the standard output device; i.e., the monitor.

This is functionally the same as
Code:
printf("Testing...\n");
 
  • #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?
 
  • #6
stellina said:
Hey guys, thank u all!

So here is what I don't understand:will fopen open the file and show its content?
fopen just opens the file. You need to use an input function such as fscanf to read the contents of each file listed on the command line.
stellina said:
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?
After you have read the contents of a given file, close it (with fclose). If you have printed the contents on the screen, they will still be there.
 
  • #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).
 
Last edited:
  • #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.
 
  • #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!
 
  • #10
Hi stellina! :smile:

stellina said:
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!

Type:

echo "I like stellina" >t.txt

on the command line, and you'll have a file named t.txt. ;)
 
Last edited:
  • #11
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.
 
  • #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.
 
  • #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.
 
  • #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!
 
  • #15
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.
 
  • #16
stellina said:
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!

Ah, you didn't mention yet that you were working on an Apple with xcode.
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! :smile:
 

FAQ: Learn How to Use File and Argc in C Programming: Step-by-Step Guide

What is C programming?

C programming is a high-level, general purpose programming language that was developed in the early 1970s. It is widely used for writing operating systems, device drivers, and other system software. C is known for its efficiency, flexibility, and portability.

What are the basic concepts of C programming?

The basic concepts of C programming include data types, variables, control structures, functions, and arrays. Understanding these concepts is essential for writing C programs and manipulating data effectively.

What are the advantages of using C programming?

C programming offers several advantages, including its simplicity, efficiency, and portability. It also has a rich set of built-in functions and libraries, making it suitable for a wide range of applications. Additionally, C is a structured language, making it easier to maintain and debug code.

What are the main differences between C and other programming languages?

C is a low-level language, meaning it provides direct access to system resources and hardware. This makes it more efficient but also more complex compared to high-level languages. Additionally, C does not have built-in support for object-oriented programming, which is a common feature in many modern languages.

Is C programming still relevant in today's technology?

Yes, C programming is still very relevant in today's technology. Many operating systems, embedded systems, and high-performance applications are written in C. It is also the foundation of many other programming languages, such as C++, Java, and Python. Knowing C can greatly enhance a programmer's understanding of how computers work at a fundamental level.

Similar threads

Replies
2
Views
4K
Replies
2
Views
2K
Replies
10
Views
2K
Replies
8
Views
2K
Replies
5
Views
3K
Back
Top