can't print txt file correctly - c

Hi i have this text file, where the first column in a character the 2nd and the third one is an integer.. but I'm not able to read and print the values correctley.
So this the file am trying to read:
c 6
o 4 3
o 2 4
o 3 2
o 1 1
o 3 3
And here is the code :
#include <stdio.h>
#include <stdlib.h>
#define N 6
int main (int argc, char *argv[])
{
int i;
int M[N];
int U[N];
char c ;
FILE* fichier = NULL;
fichier = fopen("pb1.txt","r");
if(fichier!= NULL )
{
while(!feof(fichier))
{
fscanf(fichier, "%c %d %d", &c, &M[i], &U[i]);
printf("%c %d %d \n", c, M[i],U[i]);
}
}
}
This is what the output looks like
c 6 1472131424
o 4 3
4 3
o 2 4
2 4
o 3 2
3 2
o 1 1
1 1
o 3 3
3 3
I have no clue why it gives me this. thank you

The first problem I see here, is you use the value of i uninitialized. It invokes undefined behavior.
To elaborate, i is an automatic local variable and unless intialized explicitly, will have an indeterminate value. Attempt to use that will lead to UB.
Also, you have never increased the value of i which is supposed to be used as the index for storing and printing the values.
Lastly, always check the return value of scanf() and family to ensure the success, before you try to use the scanned value.
That said, Please see Why is “while ( !feof (file) )” always wrong?

Related

How to get input for arrays whose length is not determined

I am supposed to write a program that "quicksort"s a given array of numbers. The problem is , dynamic memory allocation is not allowed and nor am I given the number of "numbers" that are supposed to be inputs of my program.
For instance, here's an input/output example:
INPUT: 0 1 5 3 6 2 4
OUTPUT: 0 1 2 3 4 5 6
But I just can't seem to be able to close my while loop when getting the numbers using scanf(); (I know , scanf() is silly , but I can't use "iostream" either)
Here's the code I was trying to get to work:
int main()
{
int target_arr[1000], l, h, i = 0, c = 0, k = 0;
while (scanf("%d", &target_arr[i]) != EOF)
{
i++;
}
h = i -1;
l = 0;
quick(l, h, target_arr);
print_arr(target_arr, i );
return 0;
}
quick() and print_arr() functions are previously defined and work perfectly well given the proper input.I just don't know how to stop the loop when I'm done inputting.
Any help would be greatly appreciated.

Not getting desired output. Is there something inherently wrong in the logic?

the input depicts no of tests followed by size of input array followed by the array and the output either gives -1 or the square of the largest prime in the given array.
I am providing the code and the expected and actual output along with sample standard input used.
standard input:
3
5
1 4 6 8 10
3
2 2 9
2
156 13
expected output | getting
-1 -1
4 4
169 -1
#include <stdio.h>
int main(){
int test,size;
int i,j;
scanf("%d\n",&test);
while(test>=1){
scanf("%d\n",&size);
int data[size],factors=0,max=0;
for(i=0;i<size;i++){
scanf("%d ",&data[i]);
for(j=1;j<=data[i];j++){
if(data[i]%j==0){
factors+=1;
}
}
if((factors==2) && (data[i]>max)){
max=data[i];
}
}
if(max>=2){
printf("%d\n",max*max);
}else{
printf("%d\n",-1);
}
max=0;
test-=1;
}
}
ok debugged and got the answer. Factors needs to be initialized inside the next for loop.

Read A file line by line into an Array in C

I'm having a lot of difficulty doing this! What I do is get the first line to initialize an array of pointers, then want to point those blocks to variables that contain the string from the text document. However; even when I read all the values into the array they are all pointing to the same variable which changes as the file is being read. Is there way I can copy those values into the array without them all pointing to the changing line as the file is being read?
int main(void){
FILE * fp;
char line[256];
int i = 0;
int digit = 0;
fp = fopen("testfile","r");
if(fp == NULL){
printf("Cannot Open File");
}
fgets(line,sizeof(line),fp);
digit = atoi(line);
printf("digit = %d\n",digit);
char *rest[digit];
while(!feof(fp)){
while (i < digit){
fgets(line,sizeof(line),fp);
fgets(line,sizeof(line),fp);
printf("line = %s",line);
char arr[sizeof(line)+1];
strcpy(arr,line);
rest[i] = arr;
printf("restaurant = %s",rest[i]);
i++;
}
the text file is as follows:
6
Outback Steakhouse
Red Robin
Max & Erma’s
Chipotle
Panera
BW3
8
Stephanie 5 3 2 4
Chris 4 6 5 1
Peter 5 2 4 1
Josh 1 4 3 6
Jessica 5 2 3 4
Al 6 4 2 3
Adam 5 1 3 2
Eric 1 4 3 5
You need to copy the values into dynamically allocated memory. strdup will do. Replace:
char arr[sizeof(line)+1];
strcpy(arr,line);
rest[i] = arr;
With:
rest[i] = strdup (line);
Also you call fgets twice.
Additionally, when line is too long, it will be not zero terminated. To make it safe always assign zero at the end of line.

I get an Unexpected Output

In my book , this code is given.They say that the output is 2 2 2 2 2 2 3 4 6 5
Please explain is this correct or not ? If not then what is the correct o/p?
#include <stdio.h>
#include <string.h>
main()
{
int c[]={2,8,3,4,4,6,7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++){
printf(" %d",*c);
++q;
}
for(j=0;j<5;j++){
printf(" %d",*p);
++p;
}
}
In first for-loop you are printing *c instead of *q:
printf(" %d",*c); // outputs `2 2 2 2 2` as first element, five times
should be:
printf(" %d",*q);
as I notice you increments q
output is 2 2 2 2 2 in first loop because of *c, c decays into address of fist element in this expression.
Edit
According to your code output should be as suggested by #ChronoTrigge (I notice latter):
First loop outputs five times 2 as I explained above
second loop will output first five elements in array a[] so output should be: 2 8 3 4 4
complete output: 2 2 2 2 2 2 8 3 4 4

Error reading test cases from a file in C

for a programming homework, I'm implementing Prim's algorithm, the format of the input file for test cases is as follows:
The first line of input will be an integer C, which indicates the number of test cases. The first line of each test case contains two integers N and E, where N represents the number of nodes in the graph and E the number of edges, respectively. Then come E lines, each with 3 integers I, J and P, where I and J represent the nodes of an edge (undirected graphs, where 0 ≤ I, J
Although even I'm starting the code (I'm new to programming) i Don´t understand why my code only reads an entry for the test cases, What am I doing wrong?
this is the code reading the file entradaA.in:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv []){
int testCases;
int numberNodes;
int numberEdges;
freopen("entradaA.in", "r", stdin);
int i, j, cont=1;
int total = 0;
int a, b, c;
scanf ("%d", &testCases);
for (i=0; i<testCases; ++i)
{
scanf ("%d %d", &numberNodes, &numberEdges); //Number Nodes & Edges
for (i=0; i<numberEdges; i++)
{
scanf ("%d %d %d", &a,&b,&c);//
printf ("%d %d %d\n", a, b, c);
}
printf ("Caso %d: Total Weight %d\n", cont++, total);
}
return (0);
}
The input file (entradaA.in) look something like this
2
7 11
0 1 17
0 2 10
0 6 14
1 2 6
1 3 1
2 3 4
2 6 3
3 4 7
4 6 10
4 5 2
5 6 9
6 9
0 1 30
0 2 30
1 3 22
1 5 33
2 3 20
2 4 33
3 4 15
3 5 20
5 4 20
You have the loop variable i modified inside the loop, which is generally unwanted. In this case, since i looped to 11 (the number of edges), it caused your program to terminate since 11 is not smaller than 2 (the number of test cases in the input).
You could use temporary variable (if you are using C++, thank you aardvarkk):
for (int i=0; i<testCases; ++i)
{
scanf ("%d %d", &numberNodes, &numberEdges); //Number Nodes & Edges
for (int j=0; j<numberEdges; j++)
Note that the int j could also be int i, but I wouldn't recommend it. Just use a variable with another name would be clearer. Or if you are in C, just drop the two int and use variables that are local to the function.
For more, you could read this.
Your code produced the following output on my machine. The only change I made was to declare the int values i, j etc. before the freopen call to make the code standard C.
0 1 17
0 2 10
0 6 14
1 2 6
1 3 1
2 3 4
2 6 3
3 4 7
4 6 10
4 5 2
5 6 9
Caso 1: Total Weight 0
It's definitely reading more than your test cases, so I'm not sure what the problem is?

Resources