Content of string array is same on all positions - i need asistance - c

im working on my project to school and i run on this issue and cant continue forward. Can you plese help me? Im geting data from text document and and trying it save line by line to string array. When i printf data to stdin they are shown how they shuld be, but when i try it to printf them out of while function, conecent of all positions in array is last line of text document.
int main(int argc, char *argv[])
{
//Validate command line arguments
if (argc != 2) {
fprintf(stderr, "USAGE : %s file.txt\n", argv[0]);
return(1);
}
//Open text file for reading
FILE *f = fopen(argv[1], "r");
//Error exit if there was a problem opening the above file
if (f == NULL) {
fprintf(stderr, "Error opening file %s \n", argv[1]);
return(1);
}
int i = 0;
char *array[1000];
char line[500];
while (fgets(line, sizeof(line), f)) {
array[i]=line;
printf("%d",i);
printf("%s", array[i]);
i++;
}
printf("%s", array[1]);
printf("%s", array[2]);
printf("%s", array[3]);
printf("%s", array[4]);
fclose(f);
return 0;
}
Output:
0U 123456789101112131415161718192021222324252627282930
1S a b c x
2S x y z
3C intersect 2 3
4C minus 2 3C minus 2 3C minus 2 3C minus 2 3
Thnx guys <3

You need to allocate memory for each line you read. Currently you only have the one line[]. For example, change the line
array[i]=line;
to
array[i]=(char *)malloc(strlen(line)+1);
strcpy(array[i], line);
This will make allocate space for each string, and copy your line to it.

Related

How can I copy some strings from file to another using c programming

I have this code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* ptr = fopen("data.txt","r");
char filename[100];
if (ptr==NULL)
{
printf("no such file.");
return 0;
}
char buf[100];
while (fscanf(ptr,"%*s %*s %s ",buf)==1)
printf("%s\n", buf);
printf("Create a file \n");
scanf("%s", filename);
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
}
It coppies full content from one file to another. I need to copy only strings that have 5 as the last character (3 column)
For example Data.txt looks like that:
Alex 10B 4
John 10A 3
Kate 10C 5
In file that I will create during execution has to be coppied only Kate 10C 5 string. I've been trying for hours but I don't know how to do this. Can you help me?
In the end of each line there is a newline character, (\n) you can use that to read line by line and copy only the ones that you want:
FILE* dest = fopen("out.txt", "w+"); // supressed null check for simplicity
char buf[100];
char* char_to_find;
// parse line by line
while (fscanf(ptr, " %99[^\n]", buf) == 1){
char_to_find = buf;
// reach the end of the line
while(*char_to_find){
char_to_find++;
}
//move one back
char_to_find--;
// if it's 5 save, if not move on
if(*char_to_find == '5' && *(char_to_find - 1) == ' '){
fputs(buf, dest);
}
}
Live demo
The problem is that the function call
while (fscanf(ptr,"%*s %*s %s ",buf)==1)
consumes the input from the input stream, so that it is no longer available for copying. You are only saving the contents of the last field, but all other data is lost.
I suggest that you read one line at a time into a memory buffer, by calling the function fgets in a loop. That way, you will process one line of input per loop iteration, and will be saving the contents of the entire line.
In every loop iteration, you can use sscanf on this memory buffer to determine whether the third field has the desired value, and if it does, then you copy the entire line to the output file. Otherwise, you do nothing and proceed to the next line (i.e. the next loop iteration).
char line[100];
//process one line of input per loop iteration
while ( fgets( line, sizeof line, input_file ) != NULL )
{
char third_field[20];
if (
//third field was successfully extracted
sscanf( line, "%*s%*s%19s", third_field ) == 1
&&
//third field contains the string "5"
strcmp( third_field, "5" ) == 0
)
{
//copy entire line to output file
fputs( line, output_file );
}
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* ptr = fopen("data.txt","r");
char filename[100];
if (ptr==NULL)
{
printf("no such file.");
return 0;
}
printf("Create a file \n");
scanf("%s", filename);
FILE* dest = fopen(filename, "w+"); // check for null like above
char buf[100];
char* char_to_find;
while (fscanf(ptr,"%99[^\n] ", buf) == 1){
char_to_find = buf;
while(*char_to_find != 0){
char_to_find++;
}
char_to_find--;
if(*char_to_find == '5'){
printf("%s\n", buf); // test ptint
fputs(buf, dest);
}
}
}

Change the logic of text file reading program to suspend output after 4 lines

So what I'm basically doing here is using a command line argument to open a file but only open it 4 lines at a time, then a prompt to print out add'l lines. I can get the file to print out but I cannot figure out how to get it to only print out a few lines at a time. This is where I'm at....thoughts?
#include <stdio.h>
int main(int argc, char *argv[])
{
char line[1000];
FILE *pt;
pt = fopen(argv[1], "r");
if(pt == NULL) return -1;
printf(argv[1], line);
while(fgets(line, 1000, pt) != NULL)
printf("%s", line);
fclose(pt);
return 0;
}
I start from strange line of your code, and then I will try to answer the question.
Statement
printf(argv[1], line);
make me curious - what you what to print, actually?
Here line is not initialized, and argv[1] can hardly be used as format line.
So I suppose it should be just
printf(argv[1]);
or
printf("Filename is %s\n", argv[1]);
As for reading from a file with name provided as argv[1] your code looks able to work, I mean your code read line by line till the end of file and prints these lines at the screen.
If you want to change this logic, e.g. read only 4 first line, add condition with counter, e.g.:
int cnt;
for (cnt = 0; cnt < 4; cnt++) // repeat reading 4 times
{
if (fgets(line, 1000, pt) != NULL)
printf("%s", line);
else
break; // stop when reading fails
}
or (I prefer this version)
int cnt = 0;
while (fgets(line, 1000, pt) != NULL && cnt < 4)
{
printf("%s", line);
cnt++;
}
Such changes allows to stop reading (as well as output), so only 4 or less lines will be shown at console screen.
Finally, for case when you want to show file by groups of 4 (or other constant value), consider the following snippet:
#include <stdio.h>
#define MAX_LINES_TO_PRINT 4
int main(int argc, char *argv[])
{
char line[1000];
FILE *pt;
pt = fopen(argv[1], "r");
if (pt == NULL) return -1;
printf("Filename is %s\n", argv[1]);
int cnt = 0;
while (fgets(line, 1000, pt) != NULL)
{
printf("%s", line);
cnt++;
if (cnt % MAX_LINES_TO_PRINT == 0)
{
int answer;
printf("[%d lines printed] Continue? (Y/N) : ", cnt);
answer = getchar(); // get user's response
while (getchar() != '\n'); // clean input buffer after getchar
if (toupper(answer) == 'N')
{
break; // stop reading the file
}
}
}
fclose(pt);
return 0;
}
Try this program with your file and ask question if something is unclear.
Changing the value in the line #define MAX_LINES_TO_PRINT 4 you can regulate maximum number of lines printed at once (before the next request to continue), e.g. #define MAX_LINES_TO_PRINT 15 make your program printing up to 15 lines.

C How to use sscanf properly support

I am currently learning reading from files in C.
Anyway, cutting to the chase:
Text file content:
123456 James Doakes; 0
987987 Dexter Morgan; 0
010203 Masuka Perv; 0
int main()
{
char accountNr[ACCOUNTNRSIZE], ownerName[NAMESIZE], enter[3];
int accountBalance = 0;
char filename[] = "breg.txt";
FILE *file = fopen(filename, "r");
if (file != NULL) {
char line[128];
while (fgets(line, sizeof(line), file) != NULL) {
sscanf(line, "%s %[^;] %d ", accountNr, ownerName, &accountBalance);
printf("%s", ownerName);
//fflushstdin();
}
fclose(file);
} else {
perror(filename);
}
return 0;
}
I wrote this to check if the name for instance James Doakes was registered correctly :
printf("%s", ownerName);
But when it prints that out it's like the stdout is still active and I can push Enter and it will type the name again. My goal is to of course be able to sscanff the number, the full name, and the last number as seperate variables. But it obviously doesn't work. I am guessing a \n gets registered as well. Dunno, I am just speculating.
What am I doing wrong? Why? And how do I solve this?
Much appreciated,
Mif
%s %[^;] %d
means a string terminated by white space, optional white space, a sequence of characters that are not ;, optional white space, then a number.
You appear to be not scanning for the actual ; character itself so that, when you try to get the number, the ; in the input stream will cause it to fail. You can see this with:
#include <stdio.h>
#define ACCOUNTNRSIZE 100
#define NAMESIZE 100
int main (void) {
char accountNr[ACCOUNTNRSIZE], ownerName[NAMESIZE], enter[3];
int accountBalance = 0;
char filename[] = "breg.txt";
FILE *file = fopen(filename, "r");
if (file != NULL) {
char line[128];
while (fgets(line, sizeof(line), file) != NULL) {
int count = sscanf(line, "%s %[^;] %d ", accountNr, ownerName, &accountBalance);
printf ("%d [%s] [%s] [%d]\n", count, accountNr, ownerName, accountBalance);
}
fclose(file);
} else {
perror(filename);
}
return 0;
}
which outputs:
2 [123456] [James Doakes] [0]
2 [987987] [Dexter Morgan] [0]
2 [010203] [Masuka Perv] [0]
In fact, even if you change the breg.txt file to be:
123456 James Doakes; 314159
987987 Dexter Morgan; 271828
010203 Masuka Perv; 42
you still get 0 for the account balance because the scanning only successfully reads two items.
Whenever you use one of the scanf-family functions, you should check the return code to ensure it's scanning the correct number of items, as in:
int count = sscanf (line, "%s %[^;] %d ", accountNr, ownerName, &accountBalance);
if (count != 3) {
fprintf (stderr, "Catostrophic failure, count is %d\n", count);
return 1;
}
The fix here is relatively simple, just use %s %[^;]; %d as the format string.
With that change, the output you see is:
3 [123456] [James Doakes] [314159]
3 [987987] [Dexter Morgan] [271828]
3 [010203] [Masuka Perv] [42]
Keep in mind you don't actually need a space before the %d (though it causes no harm). That particular format specifier skips white space before attempting to scan the number.

Converting strings and ints from input file and printing to output file

I am trying to convert strings and integers to binary using fscanf and fwrite to write them to an output file.
My input file:
a 100
ab 99
abc 98
abcd 97
abcde 96
Each space separating the string and int on each line is a tab.
Here is my main.c file where the magic should be happening (but is not):
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 30
int main(int argc, char * argv[]){
FILE *ifp;
FILE *ofp;
if(argc != 4){
fprintf(stderr, "Usage: flag %s input-file output-file\n", argv[0]); exit(1);
}
if((ifp = fopen(argv[2], "r")) == NULL){ /* error check to make sure the input file is open*/
fprintf(stderr, "Could not open file: %s", argv[2]); exit(1);
}
puts("input file open\n");
if((ofp = fopen(argv[3], "wb")) == NULL){ /* Opens output file to write in binary*/
puts("couldnt open output file\n"); exit(1);
}
puts("output file open\n");
unsigned char tempstr[MAX_LEN];
unsigned int tempint;
while(fscanf(ifp, "%u %u\n",(unsigned int*)&tempstr, &tempint) == 2){
fwrite((const void*)&tempstr, sizeof(tempstr)+1, 1, ofp);
fwrite((const void*)&tempint, sizeof(unsigned int), 1, ofp);
puts("ran loop");
}
fclose(ifp);
return 0;
}
When I run my code my while loop does not seem to be running(ie.the "ran loop" is not being output). Not sure where to go from here?
Also my calls at the command line are as follows:
./a.out main.c t1.txt t1.bin
Any help would be appreciated! Thanks!
I believe this should get rid of the seg fault.
The type should be char * instead of unsigned char *
The array should have enough space to hold the required characters plus the null terminator.
.
char tempstr[30];
unsigned int tempint;
while (fscanf(ifp, "%s \t %i\n",tempstr, &tempint) == 2 ) {
fwrite((const void*)&tempstr, sizeof(tempstr)+1, 1, ofp);
fwrite((const void*)&tempint, sizeof(unsigned int), 1, ofp);
puts("ran loop");
}
Also I guess you don't need main.c, while executing ./a.out ... etc. You can change check for argc != 3 instead of 4 in your code.

Why doesn't fgets read whole line in while loop but works in for loop?

If my fgets is in a while loop, it only returns half the string. If it's in a for loop, it returns the whole string.. Any idea why?
Code below:
FILE *fp; // File pointer
char filename[] = "results.tsv";
fp = fopen(filename, "r"); // Open file argv[1] for READ
char s[4096];
int num = atoi(fgets(s, sizeof(s), fp)); // Get first line (number of units in file)
int i;
for(i = 0; i < num; i++)
{
printf("%s", fgets(s, sizeof(s), fp)); // Prints everything
}
while (fgets(s, sizeof(s), fp) != NULL) // Loop until no more lines
{
printf("%s\n", s); // Only prints the x's
}
fclose(fp); // Close file
And the files contents:
1
xxxxxxxx yyyy eee
Where the big spaces are tabs (\t).
If I run it, I get:
For loop only:
xxxxxxxx yyyy eee
While loop only:
xxxxxxxx
Thanks.
As already diagnosed, your code 'works for me'. Here's the SSCCE I created for it. If invoked with no arguments, it uses the while loop. If invoked with any arguments, it uses the for loop. Either way, it works correctly for me. Note that the code doesn't use the return value from fgets() directly; it checks that the input operation succeeded before doing so. It also echos what it is doing and reading as it goes.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *fp;
char filename[] = "results.tsv";
if ((fp = fopen(filename, "r")) == 0)
{
fprintf(stderr, "%s: failed to open file %s\n", argv[0], filename);
exit(1);
}
char s[4096];
if (fgets(s, sizeof(s), fp) == 0)
{
fprintf(stderr, "Premature EOF\n");
exit(1);
}
int num = atoi(s);
printf("Num lines: %d\n", num);
if (argc > 1)
{
printf("For loop:\n");
for (int i = 0; i < num; i++)
{
if (fgets(s, sizeof(s), fp) == 0)
{
fprintf(stderr, "Premature EOF\n");
exit(1);
}
printf("%d: %s", i+1, s);
}
}
else
{
int i = 0;
while (fgets(s, sizeof(s), fp) != NULL)
{
printf("While loop:\n");
printf("%d: %s", ++i, s);
}
}
fclose(fp);
return 0;
}
If you use this code and it fails on your system, then you could submit your evidence. Amongst other things, you should identify the platform on which you're working, and you should give a hex dump (or equivalent) of the data in the file results.tsv. The data file I used, for example, contained the bytes:
0x0000: 31 0A 78 78 78 78 78 78 78 78 09 79 79 79 79 09 1.xxxxxxxx.yyyy.
0x0010: 65 65 65 65 0A eeee.
0x0015:
Before start reading with while loop, you have to make the position of reading from the stream(file) start at the same position where the for loop start reading
You can do it with one of the 2 ways:
1) close the file and reopen it and read the first line before starting the while loop
2) Use the fseek (as KiriliKirov said) to point at the same position where the for loop start reading. To do you have get the current position (position where the for loop start reading) with the ftell() function:
int num = atoi(fgets(s, sizeof(s), fp));
long int start_read = ftell (fp); // get the current postion //add this line in your code
.....
fseek ( fp , start_read , SEEK_SET ); // add this line in your code
while (fgets(s, sizeof(s), fp) != NULL)
The second solution will avoid the close and the reopen the file and the read of the first line.
ftell() returns the current value of the position indicator of the stream.
fseek() Sets the position indicator associated with the stream to a new position

Resources